1

How are these two different in C?

char str[] = "xyz";         // statement 1

//and 

char str[4] = "xyz";        // statement 2

According to my understanding, the first one assigns a pointer to a string literal while the second one is a character array of 4 characters (including the NULL character).

But if this is the case, how are these two stored in the memory?

4
  • 2
    They are not. Both are statically allocated character arrays of 4 elements (the fourth of which is the NUL terminator \0). Commented Nov 7, 2019 at 21:25
  • 3
    No. Both cases are equivalent. This one would be different: char *str = "xyz"; and will fall under your first description. Commented Nov 7, 2019 at 21:26
  • Have a look at the difference: godbolt.org/z/fSh5qR (the compiled code is identical) Commented Nov 7, 2019 at 21:31
  • the first one assigns a pointer to a string literal Where did you get that idea? It's totally false. Commented Nov 7, 2019 at 21:40

1 Answer 1

4
char str[] = "xyz";  

declares str as a 4-element array of char and copies the contents of the string initializer into it:

     +---+---+---+---+
str: |'x'|'y'|'z'| 0 |
     +---+---+---+---+

In this case, the size of the array is taken from the size of the initializer (3 characters plus string terminator).

char str[4] = "xyz";

does the exact same thing, except the size is explicitly specified.

In both cases, the contents of str are writable - you can modify the characters of the string, like so:

str[0] = 'X';
str[1] = 'Y';
...

The maximum length of the string is fixed, however - you can only store strings up to 3 characters long (plus the 0 terminator) in str.

If you had declared it

char *str = "xyz";

then str would be a pointer to the first element of the string literal, like so:

    +---+      +---+---+---+---+
str:|   | ---> |'x'|'y'|'z'| 0 |
    +---+      +---+---+---+---+

All str stores is the address of the first character of the string. In this case, attempting to modify any of the characters in the string leads to undefined behavior. String literals are supposed to be immutable, but they are not necessarily stored in read-only memory. Code like

str[0] = 'X';

may work as expected, or it may cause a runtime error, or it may do nothing at all.

To be safe, you should declare pointers to string literals as const char *:

const char *str = "xyz";

That way if you write something like

str[i] = 'X';

the compiler will yell at you for it, so you don't have to wait until runtime to discover the error.

Sign up to request clarification or add additional context in comments.

1 Comment

@chux-ReinstateMonica: Changed wording from "literal" to "initializer". Hopefully that's a bit more accurate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.