char c[]= "JOJO1993";
char *p =c;
printf( "%s", p+p[3] -p [1]) ;

this is a code snippet. p is a pointer and when it is used as *(p+3) means 4th character. but my question how can a char pointer can be used as pointer to first byte of any object ? can we use this example to explain this ? a little bit confused, can anyone clearify it. thanks in advance.

4 Years
Discussion Span
Last Post by nitin1
Featured Replies

Actually, the line char *p =c; means that you are making p hold address of the the first byte of c. That is p is pointing to c. So when you are printing printf( "%s", p+p[3] -p [1]) ; the whole c gets printed. Try printing separatly p, p[1], *p and experiment with it. Use %c while printing *p. You will see, when you print printf( "\n%s", *p) ; your program will crash. Try it.


yes, i knoe all this in advance. but please throw light on your last line.


In printf function, format specifier %s requires a pointer to the first character of the string as the corresponding argument. Here I'm passing only the first character, which is incorrect, as %s requires a pointer to the first character, not the character itself.


When you just use printf( "%s", p) ; it will work, as p has address to first character of c, so basically a pointer to c.


That is equal to your original post as the rest of statements have +/- 0 effect to the pointer.

(p[3] -p [1]) == 0

Edited by pyTony

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.