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.

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.

what if only p ?

p[1] and p[3] are same ord("O") so calculation has no effect.

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.

So will it be error or it will crash or what ?

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

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

pytony i didnt get what u have said

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.