Re:Are pointers and arrays equivalent in C? (2009)
by deanstag on Sunday March 22, 2015 @12:36AM
(#9245599)
Yeah. I was exploring exactly this problem when i came across this original article. Later i saw that "char arr[] = "stuff" " actually inserts assembly code that puts the value stuff into the stack. That was interesting news to me.
stackoverflow.com/questions/29184100/char-array-and-pointer-initialization-semantics/29184190#29184190
This will work fine:
arr is allocated on the stack (not heap, sorry about the mistake) and is mutable.But the following will segfault:
Strings are allocated in the read-only data segment. Technically you can only write const char* ptr = "stuff", but due to wanting to remain backward compatibile, gcc will let you write the above and not even warn you unless you use -Wall or similar.