I've encountered a very strange issue that may or may not be caused by strtok.
Let me first just say that I can work around this by writing the program a different way, but at this point I'm just puzzled as to why this is going on.
Code:
int main()
{
char* buffer[2] = {0, 0};
long size50, size10;
readFile("50000words.txt", buffer[0], size50);
readFile("10000words.txt", buffer[1], size10);
char **a;
//entryList and lookupList contain pointers to locations in buffer[0]/[1] respectively.
char *entryList[numEntries],
*lookupList[numLookups];
int entryVals[numEntries],
lookupVals[numLookups];
const char delim[] = ", \n\r";
int i = 0;
entryList[0] = strtok(buffer[0], delim);
cout << entryList[0] << endl; //Tetraheism
while(entryList[i] != NULL && i < numEntries)
{
entryVals[i] = atoi(strtok(NULL, delim));
entryList[++i] = strtok(NULL, delim);
}
cout << entryList[0] << endl; //Tetraheism
cout << entryList[1] << endl; //Chrisoms
lookupList[0] = strtok(buffer[1], delim);
cout << entryList[0] << endl; //Tetraheism
cout << entryList[1] << endl; //Chrisoms
i = 0;
while(lookupList[i] != NULL && i < numLookups)
{
lookupList[++i] = strtok(NULL, delim);
}
cout << entryList[0] << endl; //chwartzlot <---- WHAT? this string is towards the end of the buffer.
cout << entryList[1] << endl; //Chrisoms
So, I'm a bit puzzled as to exactly what could have happened here. Even more so because only pointed-to value of the first element of entryList has changed.
entryList[0] points to a location in buffer[0]. Casting entryList[0] to void* in the cout statement shows that the address pointed to changes. Can anyone explain why?