Reseting a static variable?

Thank you. I had previously tried to initialize n in several different spots (but hadn't tried to initialize after checking if str != NULL).




Last edited on
You reset it (by simple assignment) when str is not NULL and only then.
if(str != NULL) //Checking to make sure our str has not made it to the null character
The comment is not correct. You pass NULL to strtok when you want to continue tokenizing the string.

Besides, why are you trying to implement an inherently broken function? A function with non-thread-local static variables is not thread-safe and therefore is not only fairly useless, but also dangerous to any unsuspecting users who might decide to use it.
Last edited on

In this line you are setting the static variable to 0:

for(n = 0; save[n] != '\0'; n++) //Looping through static pointer variable

try this:

static int n = 0; //initialized to zero here


for(n; save[n] != '\0'; n++) //Looping through static pointer variable

static variables are only initialized once, but can be set to any value later on and it retains that value.

Last edited on
Topic archived. No new replies allowed.