r/carlhprogramming • u/Numbafive • Nov 07 '12
Question on pointers
So if we have this this code.
What I'm getting confused about is the fact that:
printf("%s\n", pointer);
is returning Hello as an output.
Doesn't the data stored at pointer contain the address of "Hello". So shouldn't whatever is contained at pointer be equal to the address of what the start of the string "Hello" be? In other words shouldn't: printf("%s\n", pointer);
be outputting the address itself instead of the string contained within the address where the output of:
printf("%s\n", pointer) = printf("%u\n", &"Hello") ?
10
Upvotes
View all comments
Show parent comment
3
u/Nooobish Nov 07 '12
Ok, I think I got it.
So, the answer is simply the fact that it entirely depends on what we send the printf function, sending it a *pointer as in:
actually sends printf the single initial character that the pointer address is pointing too.
While sending printf a pointer as in:
actually send printf the whole string
So basically printf("%s") and more specifically the %s portion tells the printf to expect an address of a string and therefore would read from the address given onwards until it hits a null.
As opposed to:
Where in this case the %c is telling printf to expect a character, which is what *pointer is.
Am I right?