I don't see any point in starting a new thread and this is "slightly" related, so here goes.
char *szMainMenuChoices[] = {
"Start Game",
"Quit Game",
"Debug String 1",
"Debug String Two",
"Debug String OVER NINE THOUSAND"
};
This is stored in memory in my program as such; {'S', 't', 'a', 'r', 't', ' ', 'G', 'a', 'm', 'e', '\0', 'Q', ...} etc. until {... 'T', 'w', 'o', '\0', '\0', '\0', 'D', ...} - There are three null characters between "Debug String Two" and "Debug String OVER NINE THOUSAND".
I was trying to get a dynamically sized main menu so I have a function that...
Input:
char* szString[] = {
"Start Game",
"Quit Game",
"Debug String 1",
"Debug String Two",
"Debug String OVER NINE THOUSAND"
};
int nStrings = (sizeof(szString) / sizeof(char *));
int getCharStringBiggest(char* szString[], int nStrings)
{
char* pszString;
pszString = *&szString[0];
int nNumber = 0;
int nBiggestString = 0;
int nCurrentString = 0;
while(nStrings != 0)
{
if(*((pszString+nNumber)+1) == '\0')
{
nStrings--;
nNumber += 2;
if(nCurrentString > nBiggestString)
{
nBiggestString = nCurrentString;
}
nCurrentString = 0;
}
else if(*(pszString+nNumber) != '\0')
{
nCurrentString++;
nNumber++;
}
}
return nBiggestString;
}
eats each individual character until it reaches the end of the string (a '\0' character) and counts them, eventually spitting out the biggest string of characters before a '\0' is encountered.
Unfortunately this function fails to give the expected return for nBiggestString which is 31 (Debug String OVER NINE THOUSAND) as there are three null characters preceding it.
Now I CAN fix the function to give an additional check that if it hasn't read the final string yet it'll just go through all the null characters until it finally does reach a non-null character and continue as normal.
Before I do this fix I have two questions. Number one is: Can this array be assigned in such a way that it is purely "String" NULL "String" NULL "String" instead of "String" NULL "String" ... "String" NULL NULL NULL "String"?
If not, is there any danger in telling my function to keep jumping over null characters until it finally hits what I expect to be the next part of a string?
For reference, here is the "to be modified" version that will jump over multiple null characters just incase anyone can spot anything blaringly dangerous with my code. It's been tested and works just fine, but that doesn't necessarily mean it will always work or work on someone else's machine.
int getCharStringBiggest(char* szString[], int nStrings)
{
char* pszString;
pszString = *&szString[0];
int nNumber = 0;
int nBiggestString = 0;
int nCurrentString = 0;
bool bNull;
while(nStrings != 0)
{
if((*((pszString+nNumber)+1) == '\0') && bNull == 0)
{
bNull = 1;
nStrings--;
nNumber += 2;
if(nCurrentString > nBiggestString)
{
nBiggestString = nCurrentString;
}
nCurrentString = 0;
}
else if(*(pszString+nNumber) != '\0')
{
if(bNull == 1)
{
bNull = 0;
}
nCurrentString++;
nNumber++;
}
else if(bNull == 1 && nStrings != 0)
{
nNumber++;
}
}
return nBiggestString;
}