if we initialise char pointer as
char *p="Hello";
compiler will search for 6 bytes and will return address of first
byte to pointer p.Then how we can print value of p as
printf("%s",p);
instead of
printf("%s",*p);
if we initialise char pointer as
char *p="Hello";
compiler will search for 6 bytes and will return address of first
byte to pointer p.Then how we can print value of p as
printf("%s",p);
instead of
printf("%s",*p);
p is a char*, so the correct format string in printf is %s
Eg printf( "string is %s\n", p );
*p is a char, so the correct format string in printf is %c
Eg printf( "First char of string is %c\n", *p );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I assume that %s will print a char at each address of 'p', starting from the first, until '\0' is found. That is why you pass the location (p) not the contents (*p)
Actually, with the code
the compiler sets aside some memory for the string "Hello", along with all other string constants used in the program. p is then assigned the address of the first char in the string, that is, 'H'.Code:char *p = "Hello";
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
we do not give address of variable while printing
[code]
char a[]="Hello";
char *p="Hello";
printf("%s",a);
printf("%s",p);//Why we are passing address of first constant string ie.H instead of *p
[\code]
Because you're printing a string of characters, not a single character. If you give printf the value of the address using *p, then how would you propose getting to the next address?//Why we are passing address of first constant string ie.H instead of *p
Just because I don't care doesn't mean I don't understand.
More examples
Code:char a[]="Hello"; char *p="Hello"; printf("%s",a); printf("%s",p); printf("%s",&a[0]); printf("%s",&p[0]); printf("%s",a+2); printf("%s",p+2); printf("%s",&a[2]); printf("%s",&p[2]);
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
One that always tickles me is this:
Code:char s[] = "hippo", *p = s+2; printf("%c\n", p[-1]);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.