Page 1 of 1

displaying 2d char array

Posted: Thu Oct 15, 2015 12:05 am
by orlanrod
Having trouble displaying the contents of a 2D char array. I can make it work with a single array, but not 2D.

This is the code. i get random data in entitydata[0][1] for some reason.

intToStr(entitydata[0][0], convertint[0], 0);
intToStr(entitydata[0][1], convertint[0][1], 0);
VDP_drawText(convertint[0], 1, 0);
VDP_drawText(convertint[0][1], 5, 0);

Re: displaying 2d char array

Posted: Thu Oct 15, 2015 7:57 am
by Stef
I don't know how you declare your char* array but seeing some others parts of your sources :

Code: Select all

const char *mytxt[50][50];
int loc;

char* getGameTxt(int txtnum, int txtnumtwo)
{
   mytxt[0][0] = "PLAY";
   mytxt[1][0] = "OPTIONS";
...
}
You cannot do that.
You have to declare your string array this way :

Code: Select all

// declare 50 string of 50 characters max (49+terminator actually)
char mytxt[50][50];
then affect it that way :

Code: Select all

// copy "PLAY" in string[0]
strcpy(mytxt[0], "PLAY");
// copy "OPTIONS" in string[1]
strcpy(mytxt[1], "OPTIONS");
you always have to use the strcpy method to copy a value into a string (char[]).
Look inside the string.h file to learn more about the available string methods :)

Re: displaying 2d char array

Posted: Thu Oct 15, 2015 5:32 pm
by matteus
Lol I'd not noticed the const on his code!

Re: displaying 2d char array

Posted: Thu Oct 15, 2015 6:17 pm
by mikejmoffitt
You can do this:

Code: Select all

const char *strings[] = {
  "A C String",
  "Another C string",
  "A final C string"
};
You can then refer to strings[0], strings[1], strings[2], etc and do

Code: Select all


VDP_drawText(strings[0], 1, 0);
VDP_drawText(strings[1], 5, 0);

// etc

When I use static arrays like this, I like to null terminate the last element (make the last one a zero) so I can easily iterate through at the start and establish the length of the array. Of course, if you need a zero in there, you can create your own unique terminator, or just know the size and be careful.