Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It has mainly to do with initialization.</p> <p>The initialized variables are usually not initialized by code which says "put this value to that location", but by a definition which loads a specific value range, the <code>.data</code> resp. <code>.rodata</code> segment, to the memory location where it is supposed to be. This is done by the OS file loader. (Strictly speaking, this is not a property of C, which doesn't know anything about that, but of the execution environment.)</p> <p>That said, it is not possible to tell a part of this memory area to be copied from another one. But it would be possible that te compiler itselfs recognizes the intent of the declaration and puts the same values to diefferent locations. But that would probably be too much "guessing".</p> <p>In your case: Wouldn't a pointer to <code>FooZero</code> maybe a better solution? The values are both the same...</p> <pre><code>typedef struct Foo { int a; int b; } Foo; static const Foo FooZero = { 0, 0 }; typedef struct Bar { Foo * foo; int c; } Bar; static const Bar BarZero = { &amp;FooZero, 0 }; </code></pre> <p>Or the other way round:</p> <pre><code>typedef struct Foo { int a; int b; } Foo; typedef struct Bar { Foo foo; int c; } Bar; static const Bar BarZero = { { 0, 0 }, 0 }; static const Foo * FooZero = &amp;BarZero.foo; // if that is possible, untested... </code></pre> <p>In the first case, you would have to access <code>BarZero.foo</code>'s components with <code>-&gt;</code> (like <code>BarZero.foo-&gt;a</code>),</p> <p>in the second case you would have to access <code>FooZero</code>'s components with <code>-&gt;</code> (like <code>FooZero-&gt;a</code>).</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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