I'm a little confused as to how C treats int*
differently from char*
. For example, in section, we see things like:
int x = 5;
int* ptr_x = &x;
So, here, ptr_x
points to the address in memory of an integer. However, I already set up the variable in memory.
When dealing with strings, it seems like we do:
char* s = "Hello";
Instead of something more analogous like:
char s = "Hello";
char* s ptr_s = &s;
Now, I know that char
's can't contain more than one character (and so the previous block of code doesn't work). But, what's going on under the hood? Is s
just the address of the H
, and C then puts the other letters in blocks of memory next to it? Is the pointer a pointer to all 5 letters (perhaps like an array of pointers)?
I thought it might be just the first letter, but then the following happens:
char* s = "Hello";
printf("%s\n", s);
'Hello'
So, the printf
function knew to look for all the letters. Is it that char*
functions know to look for the \0
char as well, and that is what's happening?
Perhaps a related question -- if pointers are pointers to address in memory, then ptr_x
as above makes sense to me. Here, char* s
looks like it contains letters, not an address.