and now, forever, you know its length. If you add or remove frome it, change the value. A little more work for the programmer up front, but a lot less weirdness every time you use the thing. A little nicer, you can use an enum to name each index:
enum eforfoo {title, name, date, whatever, efmax};
char* foo[efmax]= {...}; //this auto increments, just add entries to the enum before the last one that gives its size and recompile.
....
printf(.. ,foo[title] ..);
Thank you jonnin I tried even this .. and I know it works, but I don't want to everytime I add a new line to change the MACRO number, I just want it to change using the strlen function, but this "" return <-- nothing from nothing isn't it?
So i need to build something that tells the strlen function when I have the empty quotation to count it as 1 string. Otherwise is simply enough to do this
1 2 3 4 5 6 7 8
#define LINES 3
char gCreditsEn[LINES][MAX_PATH] =
{
"Title",
"",
"Thank you for playing..."
};
This makes no sense. *gCreditsEn means gCreditsEn[0]. gCreditsEn[0]'s string length is 5 (6 if you include the null pointer). Show a minimal example that reproduces your issue.
Also, that's not standard C++. String literal pointers need to be const. (Edit: I suppose you're writing in C? Still, you should use const.)
without the 7, 8, 9 there are 8 lines in the array of the pointers..
But yea.. maybe I am wrong to calculate how many strings are with strlen.
Now I realized I did a mistake.
Well then the question is how do I know at run time how many strings are in the array of the pointers without using a MACRO and write it at Compile time ??
#include <stdio.h>
#include <string.h>
constchar *gCreditsEn[] =
{
"Title", //0
"Game created by:", //1
"Game Design:", //2
"Music by:", //3
"Sounds by:", //4
"Game engine:", //5
"Game Testers:", //6
"", //7
"", //8
"", //9
"Thank you for playing..." //10
};
int main(void)
{
int num_lines = sizeof(gCreditsEn) / sizeof(char*);
for (int i = 0; i < num_lines ; i++)
{
if (strlen(gCreditsEn[i]) > 0)
{
printf("%s\n", gCreditsEn[i]);
}
else
{
printf("[empty]\n");
}
}
return 0;
}
I'd put "[C] rest of title" or "C - rest of title" as your title so that people immediately read the "C" part first. That way you'll get less people suggesting you to use vector<string> and such.
Okay that's what I was looking for. Thanks Ganado.
That doesn't mean the other guys don't know the answer, is just me being NOT very explicitly in what I want to obtain , and this sizeof(gCreditsEn) / sizeof(char*); I wrote it soo many times in other code, I don't know what's in my had trying to find the elements of the array of pointers using strlen. Dohh, maybe I need to take a brake sometims :D
You don't need to use possibly expensive strlen() to see if a string is empty or not. If you have the pointer to the string then just test the dereferenced pointer for not 0 if the string is not empty. Consider: