Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Try printf("%c", 1["Hello"]). You might be surprised. The swap 1 and the string literal and try again. Note: That is just to make you think - do not use this in your code. You will find the answer here already, so please do not add a new one. Commented Aug 11, 2015 at 21:39
  • in C, an array degrades to the address of the array. In the statement: 'char* s = "my String";' the char string 'my String' +'\0' is placed in a readonly memory segment. and 's' receives a pointer to that char string (I.E. the address of that string) The char string cannot be modified, but can be read. Commented Aug 11, 2015 at 22:19
  • 1) Could use char s[] = "my string"; and s is right-sized to 10. 2) &s is the address of a pointer to an array of 10 char - not the same type as char *. Commented Aug 11, 2015 at 22:22
  • @user3629249 "placed in a readonly memory segment" and "The char string cannot be modified," are not specified by C. Attempting to modify it is undefined behavior. We agree code should not attempt/expect modifying to work. Commented Aug 11, 2015 at 22:25
  • For 3), s is a variable, as such it needs memory (as any normal int would). It happens that it points to an address, but thats just a detail. No additional space is required, if you lose that address, its lost for good, no one keeps track of it. Then, s is a variable (that consumes 4 bytes), that points to a piece of memory that has 10 bytes. Hope this clarifies a bit.
    – Leonardo
    Commented Aug 12, 2015 at 7:11