0

i encountered the problem below, anyone can explain to me, thanks.

for (;*sPtr!='\0';sPtr)

cout << *sPtr;

how to understand for loop ";"? thanks

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Lerner
0

the ; just means it is being to taken from where sPtr currently is pointing to
the first value in a for loop is just assigning a value to start looking from

for(i=0; i<10; i++)
{}

would be the same as

int i = 0
for(; i<10; i++)
{}

also you can have an infinite loop

for(; ;)
{}
0

the ; just means it is being to taken from where sPtr currently is pointing to
the first value in a for loop is just assigning a value to start looking from

for(i=0; i<10; i++)
{}

would be the same as

int i = 0
for(; i<10; i++)
{}

also you can have an infinite loop

for(; ;)
{}

because the orignal code like this below:

void printCharacters(const char *sPtr)
{
for(;*sPtr!='\0';sPtr++)
cout << *sPtr;

i mean in the first ";" in for loop, how to understand it ? thanks
}

0

sPtr is the a pointer to the first char of the array of char being passed to the function. and *sPtr is the same as sPtr[0]. You could write it this way I suppose.

for(sPtr = &sPtr[o];*sPtr!='\0';sPtr++)

but that seems a little clunky, even if it is more explicit. BTW I haven't actually tested my answer rigorously, so it's just my best guess.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.