Click to See Complete Forum and Search --> : Weird struct thing
CopyFileW
September 8th, 2007, 05:30 AM
stEntries stEnts[]={
{ "TestString", bTest},
{ "TestString2", bTest2},
{ "TestString3", bTest3},
};
iEntries = sizeof(stEnts)/sizeof(stEntries);
for(int x = 0; x <= iEntries ; x++)
{
Log("For Loop %i, %s", x, stEnts[x].szName);
}
.. This outputs this to the log...
iEntries 2
For Loop 0, TestString
For Loop 1,
For Loop 2,
Does anyone know why this is happening and how to fix this?
g3RC4n
September 8th, 2007, 08:46 AM
don't know the struct, and do you mean the problem is stEnts[x].szName is only logged once?
kirants
September 8th, 2007, 02:01 PM
Please post all relavant code. BTW, it iEntries is equal to 2 as you say, why is the loop being executed 3 times ? You should change the condition from x <= iEntries to x < iEntries
CopyFileW
September 8th, 2007, 02:59 PM
typedef struct _stEntries {
char szName[256];
BOOL bCheat;
}stEntries;
don't know the struct, and do you mean the problem is stEnts[x].szName is only logged once?
yes
kirants
September 8th, 2007, 03:37 PM
You cannot assign a string to a char array like
szName = "teststring"
You have to copy the string using strcpy or something like that.
or
You can make szName a char* instead and then you are good. Because now, you are just pointing it to a location.
Another option is to use std::string class instead of char arrays.
You are going out of bounds on the array by using the <= instead of < in your for loop as I have already mentioned.
googler
September 8th, 2007, 05:58 PM
You cannot assign a string to a char array like
szName = "teststring"No, but you can initialize a char array with a string literal.
The fact that iEntries is 2 instead of 3 as it should be shows that the code being run is not the same as the code posted.
PS what is the type of bTest, bTest2 and bTest3?
kirants
September 9th, 2007, 12:19 PM
No, but you can initialize a char array with a string literal.
My bad. Sorry :(
The fact that iEntries is 2 instead of 3 as it should be shows that the code being run is not the same as the code posted.
PS what is the type of bTest, bTest2 and bTest3?
Yeah. There was a disconnect between the log and the code. Hence I asked for all relavant code.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.