Archived

This topic is now archived and is closed to further replies.

Fallen_Angel

Simple array problem.

Recommended Posts

Fallen_Angel    138
I''m trying to add a font class to my game. The way I''m trying to get it to work is by passing a string to the font class and then compare it to a pre-determined array to get the position, which will then be used to draw the right character sprite. Is this the right way of going about it? At the moment i''m declaring char heading[28]; in the header file and using heading[28] = (char)"BCDGLPSTWY12acdeklmnorstuvwy"; in the constructor to fill it. I''m getting no compile errors yet it''s not running. It''s probably something simple no doubt but for the life of me i just can''t see where it''s going wrong. Thanks in advance.

Share this post


Link to post
Share on other sites
Martee    476
It looks like you are trying to get the arry heading[] to contain that string. If that is the case, you''ll need to do it like this:
strcpy(heading, "BCDGLPSTWY12acdeklmnorstuvwy");

~~~~~~~~~~
"Usewhitespacetoenhancereadability" - Steve McConnell, Code Complete

Share this post


Link to post
Share on other sites
vbisme    100
to my knowledge...

char heading[28]; //declares an array of chars with 28 elements.

heading[28] = (some char);
//this line is invalid because it''s supposed to set element 28, which is the 29th element in the array, which does not exist since elements start with 0. And besides, since it''s supposed to assign to only a single character, it cannot be assigned with a string.

I could be wrong.

Share this post


Link to post
Share on other sites
Inmate2993    222
Reread the portion of your C tutorial about pointers.
This--
char *hellomessage = "Hello";
creates a pointer that points at the H.

hellomessage = "Boo.";
This changes the hellomessage pointer to point at the B.

hellomessage = boomessage;
This makes both pointers point at the same block of memory.

Now, a char something[32] creates BOTH a 32 byte amount of space, AND a pointer named something of type const char *. If you want to put a string in there, use the strcpy() function or something, but don''t same something="string."; or you''ll get a compile error.

:: Inmate2993
:: William C. Bubel
"Please refrain from bothering Booster."

Share this post


Link to post
Share on other sites