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

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

cout << *sPtr;

how to understand for loop ";"? thanks

Recommended Answers

All 3 Replies

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(; ;)
{}

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
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.