Saturday, 15 May 2010

c - while(*p++) loop doesn't print the first element of the string -



c - while(*p++) loop doesn't print the first element of the string -

i have stored string in pointer variable has memory in heap section. when print string character character first element skipped.

example: if char *p="hello" when print character character using while(*p++) loop 'h' skipped , output "ello" please can 1 explain it?

#include <stdio.h> #include <stdlib.h> int main() { char *p=(char*)malloc(sizeof(char)*20); printf("enter string\n"); scanf("%s",p); while(*p++)// value assign , address increased { printf("%c",*p);//why first character skipped?? } homecoming 0; }

because while(*p++) increments value loop starts. time printf statement reached, p has been incremented.

p = 0 while(p++) { // p = p + 1 printf('%d', p); // p = 1 when executes }

c pointers

No comments:

Post a Comment