Jump to content
  • Advertisement

Archived

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

Fallen_Angel

Simple array problem.

This topic is 6069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
You might as well use char heading[] (variable-length string) or strcpy(). You can''t initialize a fixed-length arry with the assignment operator.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!