-1

I am struggling to find a way to write the alphabet letters in a char* using for. If I only had to do that using a table of chars, it'd be like this

for(i=0;i<26;i++) {
    character[i] = 'a' + i;
}

But I have a function that requests a char *, so I have to use "something" instead of 'something'. Can you help me?

Thank you!

EDIT

Sorry I wasn't clear before. It seems I am quite tired.

The XPM structure is at it follows:

struct XPM {

    unsigned int width;
    unsigned int height;
    unsigned char cpp;
    unsigned int ncolors;
    Color *colta;
    unsigned int *data[]; 
};

So, the function I have to use is the following:

void setXPMColor(XPM *imagine,
    unsigned int index,
    unsigned char r,
    unsigned char g,
    unsigned char b,
    char *charpattern) {

    imagine -> colta [ index ].r = r;
    imagine -> colta [ index ].g = g;
    imagine -> colta [ index ].b = b;
    imagine -> colta [ index ].pchars = charpattern;
}

In the main() function, I have the following line of code:

    for( i = 0; i < NCOLORS; i++ ) {
        setXPMColor( image, i, 5 * i, 0, 0, SOMETHING);
    }

What can I put insead of SOMETHING in the function, so for every index, I'll have a different character? (I was thinking of putting a new letter or any kind of symbol for each entry). Later I will have to use the symbols to generate an image.

So, in the end I would want something like:

printf("%s\n", imagine->colta[0].pchars); //a
printf("%s\n", imagine->colta[1].pchars); //b
printf("%s\n", imagine->colta[3].pchars); //c
//etc

EDIT 2:

I will have to generate an XPM file that will create an image, so I will have to draw the characters in the file. The problem is giving the function setXPMColor the character because it needs a char* if I send it for e.g. 'a' it will say type mismatch, the function requires a char* but it receives an int. It works only if I send it the letter in block quotes like "a", but then I cannot increment the letter on each iteration. I hope it helps!

Thank you!

Sebastian Luke
  • 385
  • 5
  • 18
  • 1
    Huh? Isn't `character` a `char *`? – David Schwartz Feb 27 '14 at 23:41
  • If I pass it to the function, it says it's an integer type and the function needs a character* – Sebastian Luke Feb 27 '14 at 23:42
  • 1
    Please [edit] your question to make it more clear what problem you're asking us to help you solve. It's very unclear to me what your actual question is here. When you do so, please remember that we can't see your screen or read your mind, so the only information we have to go on is what you give us in your question. If you can't state the problem and question clearly, it's unlikely we'll be able to help you. Thanks. – Ken White Feb 27 '14 at 23:43
  • 1
    You're going to have to be very clear and explicit or nobody will know what you're talking about. If you pass what to what function, what says what's an integer type? How about showing us the exact code that's giving you a problem and the exact error message you get? – David Schwartz Feb 27 '14 at 23:43
  • Sorry for the ambiguous text from before. I wrote the whole explanation – Sebastian Luke Feb 27 '14 at 23:50

2 Answers2

1

A char* is a pointer to a single character, or an array of characters, you your existing code will work. Just make sure that the char* you're passing into the function is at least 26 elements long, otherwise you will modify memory outside the array (which may or may not crash your program).

You can create such an array like so:

char * my_character_array = (char*) malloc(26);
DXsmiley
  • 529
  • 5
  • 17
0

I'm not sure if this is what you are asking but I would try converting to and from Ascii http://web.cs.mun.ca/~michael/c/ascii-table.html

that will allow you to cycle through the letters like they were numbers

BSteele
  • 11
  • 3