7

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.

2 Answers 2

4

what's going on under the hood?

when having

char *s = "hello";

basically an array of chars is created for you and is initialized with the contents of the string literal (in this case, "hello"). The address of the location, where this array is stored, is then stored into s.

Technically, this is different because this is a syntactic sugar that C provides to make the process of creating strings easier since they're used frequently.

Otherwise, you would create a string maybe in one of these ways:

char *s = malloc(sizeof("hello"));
strcpy(s, "hello");


char s[] = {'h', 'e', 'l', 'l', 'o', '\0'};


char s[sizeof("hello")];
s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
s[5] = '\0';

so, thankfully, C has this syntactic sugar that makes our lives easier.

Is s just the address of the 'H', and C then puts the other letters in blocks of memory next to it?

yes.

Is the pointer a pointer to all 5 letters (perhaps like an array of pointers)?

typically, each byte in memory has an address. essentially, an array takes up a block of memory (a byte or more). the address of a memory block is the address of the first byte in that block.

what's happening? given the address of the first character of a string (which people kinda imprecisely refer to as the string itself -- in the previous example, it would be s) and the "%s" format specifier, printf keeps printing the contents of the memory location whose address is current until it hits a '\0'.

for example:

address:    100  101  102  103  104  105
content:    'h', 'e', 'l', 'l', 'o', '\0'

given the address 100 and the "%s" format specifier, printf prints the contents of location 100 which is the 'h' in this case, then prints the contents of location 101 which is the 'e', ..., then it gets to location 105 and it finds that the content is a '\0' so it stops.

Here, char* s looks like it contains letters, not an address.

as explained above, it doesn't actually. thanks to the syntactic sugar, some actual work is done for you underneath the hood that ends up storing the string somewhere in memory and storing the address of that memory block into s.

-1

Great question, I'm also learning, but I'll do my best to answer.

In the case of char* s = "Hello"; the "Hello" is known as a string literal. This means it gets stored in special spot in memory that is read only. The specifics of exactly where it points depends system by system. But it not the same as storing an item in a variable. Note the difference between char 'x' and string "x".

Being in read-only memory, it means that that you cannot modify the string literal, but you can use it like normal string array for just reading. printf("%s", s) would work fine for example. It would read from the first character of the string literal and continue until it reaches an a null character \0.

Compare this all to char s[] = "Hello"; Which puts the string literal in read-only memory, then loads it into the array of chars.

http://www.cprogramming.com/tutorial/c/lesson9.html

https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .