Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you define and initialize a pointer to char like this:</p> <pre><code>char * word = "hello"; </code></pre> <p>you are actually telling the compiler to put the fixed string "hello" into a fixed piece of storage somewhere and then create the <code>word</code> pointer variable to point at it.</p> <p>Although you change change the <code>word</code> variable to point at something else and if it did point at some mutable storage you could change what it points to via * and [] operators, you are not allowed to change the fixed string "hello" through it.</p> <p>C++ allows assigning a fixed string to a pointer to non-const <code>char</code> purely for backwards compatibility. It is much better to only assign these strings to pointers to <code>const char</code>. e.g.</p> <pre><code>const char * word = "hello"; </code></pre> <p>This way you prevent causing illegal run time behaviour through compile time type checks.</p> <p>Edit:</p> <p>In your example there is essentially no externally visible difference between having the local variable declared static and not. This affects the life time of the pointer variable in each function. It does not affect the lifetime of the fixed strings that the pointer variables point at. As the functions return the <em>value</em> of the pointer variable (in C++ returns are always by value), it doesn't particularly matter whether the pointer variable in the function is destroyed at the end of the function or not. The strings themselves will always live beyond the scope of the function as string literals have static storage duration.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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