Hi,
All strings are arrays of char's.
The name of the array is a pointer to the first element of the array.
printf("%s",pntr)(NOTE %s) treats the pointer to char as a string pointer and will output all the char's till it finds a \0.
In other words in the two statements below pntr and array are both pointers.
Actually, they are not both pointers. An array decays to a pointer in _certain contexts_ (such as when passing an array to a function). But they are wholly different things.
For example, you can't do this:
char a[20];
char *b="hello";
a=b;
But you could do this:
char *a;
char *b="hello";
a=b;
Or, these statements don't yield the same results:
I don't mean to be picky -- your statements would be correct if you qualified them with "in certain contexts," -- I just wanted to clarify that because I think that a lot of confusion with C stems from statements that suggest that arrays and pointers are the same thing; they're not.
At any rate, I don't feel that the OP's question (whatever it really was) has really been answered.
Ok, I think I understand what was being asked in the 1st place. The OP was wondering why you didn't have to allocate memory (not dereference) for the pointers.
The compiler sets the pointer to point to string literals in the example provided. It's implementation defined as to how the compiler does this and where the actual string literals reside in memory. The caveat here is that you may not modify what these pointers point to.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.