Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you put a definition <code>char *str;</code> inside a function, then it's an automatic variable of type "pointer-to-<code>char</code>".</p> <p>It's located on the stack, and that part of the stack becomes unused when the function returns (the compiler handles this for you by emitting code to move the stack pointer as necessary). In theory the existence of a stack is purely an implementation detail, in practice C always has a call stack, almost all implementations manage that more or less the same way, and however it is actually managed, the memory in which automatic variables is stored is liable to be referred to as "the stack".</p> <p>If you put a definition <code>char *str;</code> outside any function, then it's a global variable with static storage duration.</p> <p>It is stored in a read-write data segment, and becomes unused when the program exits (probably the OS handles this for you, although it could in principle be code emitted by the compiler). Since it is zero-initialized (and assuming an architecture on which a null pointer is represented by all bits zero) yes, it can go in the bss segment, which is specifically for zero-initialized read-write objects with static storage duration. Again, the details of how objects of static duration are stored is up to the implementation but again, this is how its generally done.</p> <p>None of this has anything to do with string literals being unmodifiable, because you haven't defined a string (let alone used a string literal). You've defined a pointer, which could point to a string but does not (yet) do so.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload