3 ms·
Does this mean that `word` in char *word = "xyz"; is a pointer to an array of four `int`s, `'x'`, `'y'`, `'z'`, and `'\0'`? When I evaluate sizeof(*word)
by nequo 3y ago
Does this mean that `word` in
char *word = "xyz";
is a pointer to an array of four `int`s, `'x'`, `'y'`, `'z'`, and `'\0'`? When I evaluate
sizeof(*word)
I do get 1 instead of 4, even though `*word` is pointing to `'x'`. Where are the remaining 3 bytes in memory?
- lucozade 3y agoIf you specifically type it as char * the it's a pointer to chars each of which has size 1.
- seba_dos1 3y agoNot at all. There are no character literals in "xyz", this is a string literal and it's unrelated to what your parent was saying.
- jcranmer 3y agoA char is 1 byte by definition. But the type of a character literal (the 'x' syntax) is not a char, but an int instead. The C type system generally matters so little that the type of an expression has little relevance (sizeof is the most notable exception to that rule), which obscures this fact.
- kzrdude 3y agoyou'll have to understand the 'x' syntax and the "xyz" syntax as two different things. Different quotes.
- nequo 3y agoI know. But my understanding was that `"xyz"` is an array of characters so that these two would have the same representation in memory: char word[] = {'x', 'y', 'z', '\0'}; // sizeof(word) = 4, sizeof(*word) = 1 char word[] = "xyz"; // sizeof(word) = 4, sizeof(*word) = 1 What I did not realize was that the above two are not the same as this: char *word = "xyz"; // sizeof(word) = 8, sizeof(*word) = 1
- kzrdude 3y agoWhat I see there is that you seem to highlight the difference between using sizeof with an array and sizeof with a pointer, which makes a difference, even if array-decays-to-pointer is a rule in most other contexts.
- nequo 3y agoRight, I am mixing up two things here. You are right that bringing up pointers here is a mistake. But apart from that, I would expect `{'x', 'y', 'z', '\0'}` to have size 16 rather than size 4 because it consists of four character literals which each have size 4 on my machine.
- kzrdude 3y agoMaybe do not overthink it. 'x' is called a character literal, but it has the type int. `{'x', 'y', 'z', '\0'}` does not have a type by itself, but it's valid syntax to use it to initialize various structs and arrays - some of those will have the size you are looking for, depending on which type of array or struct you choose to initialize with that: https://gcc.godbolt.org/z/Tqjq3xzKo https://gcc.godbolt.org/z/Tqjq3xzKo
- nequo 3y agoThank you for the explanation and the Godbolt example! I appreciate it. Apologies for fumbling around in confusion.
- peterfirefly 3y agosizeof() returns the number of "units" that something -- an expression or a type -- takes up. What do you think those units are? They are literally defined as "characters". sizeof(char) is always 1. Your confusion (besides the pointer thing) is that 'x' is a funny way to write an int, not a char.
- nequo 3y ago
- _kst_ 3y agoword is of type char*, a pointer to a (single) object of type char. The initializer means that the char object it points to happens to be the first (0th) element of an array containing 4 elements with values 'x', 'y', 'z', and '\0'. Most manipulation of arrays in C is done via pointers to the individual elements, and arithmetic on those pointers. (Incrementing a pointer value yields a pointer to the next element in the array.) For example, `sizeof word` gives you the size of the pointer object, but `strlen(word)` yields 3, because it calls a library function that performs pointer arithmetic to find the trailing '\0' that marks the end of the string. (A "string" in C is a data layout, not a data type.)