Use of Pointers

I recently came across a code that i didn't really understand.

1
2
3
4
5
void putstr(char *s)
{
   while(*s)
      putchar(*s++)
}




I didn't understand the meaning of while(*s) in the code. Can anyone please explain that ?
It's equivalent to while (*s!='\0')
The s pointer contains the memory address where a character is stored.
The while (*s) checks that the character is not aka '\0'.
The putchar(*s++); increases the address to the next element with delayed action then the * dereferences and the character gets printed.

Delayed action means that the incrementation will not evaluate for the current instruction.

1
2
3
int a=5;
cout << a++; // will print 5!
cout << ++a; // will print 7! 

Typical C programming code. For beginners, below would be clearer.

1
2
3
4
   while(*s) {
      putchar(*s);
      s++; //this line by itself has no difference to ++s or s++ but *s++ and *(++s) have difference
  }
Topic archived. No new replies allowed.