Ok. I simplified the code down a lot to something manageable. Here's the problem. You see this code posted below? Well pretend the array char* p[50] contains three strings. the first element in the array is "ho" the second is "ha" and the last one is "he"
Now when I first print p[1] (the second element in the array) it prints "ha" and only "ha", but when I print p[1] for the second time it prints "ha he" (both the second and third element. Does anyone know why this is?
if you have to assume the variable word is an integer that starts off at the value 3
Thanks in advance everyone. I could really use some help on this. Been scratching my head trying to figure this mess out for weeks, and I finally get it to the point where it should work, except its screwing up, because of this....
Why does the array of 50 strings only contain 3? And i'm assuming you mean the first 3 contain strings.
(more to come maybe)
Originally Posted by zeco
Why does the array of 50 strings only contain 3? And i'm assuming you mean the first 3 contain strings.
(more to come maybe)
To handle overflow, but the outside significance of this code isn't importance, in fact if I explained more it would simply overcomplicate things.
And yes ofcourse the first three elements
p[0] = 1st element , p[1] = second element and p[2] = 3rd element.
Originally Posted by Matrix_NEO006
p[0] = 1st element , p[1] = second element and p[2] = 3rd element.
No comment at the above.
Eitherway, The strings in the array aren't being changed at all, so maybe its how it's being displayed, But that doesn't make sense either.
Yes, but the important thing to realize is this:
p[1] at first = "ha"
then the second time I call p[1]
p[1] = "ha he"
if you don't believe me try this yourself... and giving stand in values for the variables.
Originally Posted by why06
Yes, but the important thing to realize is this:
p[1] at first = "ha"
then the second time I call p[1]
p[1] = "ha he"
if you don't believe me try this yourself... and giving stand in values for the variables.
Yeah i understand that... I'm trying to figured it out right now too... But looking at your code makes me sleepy. And i was about to elaborate on my above post....
I'm tired too... yawn... it probably has something to do with strcpy() oh well I'll look into it tomorrow when I can focus >_>....
-.- Why06, i hate you. Because of your program i realized I forgot some of how multi-level pointers worked....and ended up spending half an hour going crazy to reteach myself. It's all good now though.
Anyway the only thing i can think of is if somehow the null character between p[1] and p[2] dissappeared
Is it possible that first time it works but maybe cuz of coding the second time it adds on to first and reads as p2?
Originally Posted by zeco
-.- Why06, i hate you. Because of your program i realized I forgot some of how multi-level pointers worked....and ended up spending half an hour going crazy to reteach myself. It's all good now though.
Anyway the only thing i can think of is if somehow the null character between p[1] and p[2] disappeared
possibly, I'll have to look into that... lol sorry I kept you up... I'm sort of like that too, once I get working on something its hard for me to stop. until I figure it out...
Originally Posted by bmw
Is it possible that first time it works but maybe cuz of coding the second time it adds on to first and reads as p2?
It's more then possible. That's what the problem is, but thanks for generalizing it down like that.
Here see I simplified it even more... so it definitely has something to do with the strcpy() and strtok() functions...
[php]
cout<<"p[1] " << p[1]; // this will print "he"
while(words > 0)
{
strcpy(init_str, str);
temp = strtok(init_str, " \"");
cout<< p[1]; //this will print "ha he"
}
//Assume the following conditions before the code executes:
//temp, init_str, and str are all char*
//temp = "";
//init_str = "";
//str = "ho ha he";
//p[0] = "ho"; p[1] = "ha"; p[3] = "he"
[/php]
Ok. Maybe I simplified it a little too much this has grown far more complicated then I initially suspected. Now I'm of the suspension that it is p[1]'s link to init_str that is causing the problem. I will demonstrate
[php]
char* p[50];
int words = 0;
char* init_str;
strcpy(init_str, str);
char* temp = strtok(init_str, " \"");
while(temp != NULL)
{
p[words] = temp;
temp = strtok(NULL, " \"");
words++;
}
cout<<"p[1] " << p[1] <<endl;
while(words > 0)
{
strcpy(init_str, str);
char* temp2 = strtok(init_str, " \"");//Here I declared a completely new temporary string, just to test weather temp was the problem.
cout<<p[1]<<endl; // Its not. It has something to do with p[1] connection to init_str through temp...
}
[/php]
Im so screwed....
Try erase the second "str" in the strcpy line
maybe this is the problem...
i think it isn't but you can try it.
the end of the string is a zero i think that is a delimiter to but there is a possibility it isn't
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token.