DEV Community

Discussion on: Why use pointers at all.

 
pentacular profile image
pentacular

Actually, they're not const char *.

A string literal produces a char array, so the type of "hello" is char[6], which evaluates to a value of type char *.

It is undefined behavior to mutate an object produced from a string literal, so it would be nice if the type system did help you out with a const, but backward compatibility ... :)

Note that there is no heap or stack in C. C has allocated, static, auto, and register storage instead.

Objects produced by modifying string literals can be allocated however the compiler likes, providing they have sufficient storage duration that nothing will ever notice them not being there anymore.

Because modifying them has undefined behavior they're often allocated in a read only part of the program's storage, like functions.

They may also be consolidated -- so you cannot expect "a" and "a" to produce distinct objects (but you also cannot rely on them not doing so).