title: Is it safe to return a struct with pointer members that point to internal struct arrays?
body: <p>If we have pointers in a struct that point to other members of the struct, is it guaranteed these pointers will still point to the internal member of the struct if we return the struct from a function?</p>
  
  <p>Take for example this small-size-optimized buffer struct:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  
  struct buffer {
      uint8_t *begin;
      uint8_t *end;
      uint8_t sso[20];
  };
  
  struct buffer test() {
      struct buffer buffer;
      buffer.begin = buffer.sso;
      buffer.end = buffer.begin + 20;
  
      return buffer;
  }
  
  int main(void) {
    struct buffer buffer = test();
  
    return buffer.begin == buffer.sso ? 0 : 1;
  }
  </code></pre>
  
  <p>When I compile this code locally and run it, it returns 0 so <code>buffer.begin</code> still points to the start of <code>buffer.sso</code>. However, is this guaranteed to always be case or am I depending on compiler-specific RVO behaviour?</p>