Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Container</code> holds a pointer to <code>struct intArray</code>, which in this case is the start of an array of <code>struct intArray</code>. But then you initialize this pointer with</p> <pre><code>malloc(sizeof(intArray *) * Some_Defined_Value); </code></pre> <p>So malloc returns a pointer to a memory space which holds <strong>pointers</strong> to <code>struct intArray</code> because you used <code>sizeof(intArray *)</code> and not <code>sizeof(intArray)</code> (you allocated space for <code>Some_Defined_Value</code> number of <strong>pointers</strong>). </p> <p>You need <code>sizeof(intArray) * Some_Defined_Value</code> here to allocate space for <code>Some_Defined_Value</code> number of <code>struct intArray</code>'s.</p> <p>Then you use</p> <pre><code>Initializer(&amp;myContainer, 5); </code></pre> <p>which should at least give a warning because you pass a <strong>pointer</strong> to a <strong>pointer</strong> to a <code>struct Container</code>, but <code>Initializer</code> expects a <strong>pointer</strong> to a <code>struct intArray</code>, so this is not what you want. To initialize the first element of your array of <code>struct intArray</code>'s use:</p> <pre><code>Initializer(&amp;(myContainter-&gt;P[0]), 5); </code></pre> <p>Then:</p> <pre><code>printf("the data that should be at index 0 -&gt; %d\n", myContainer-&gt;P.array[0])); </code></pre> <p>This code does not compile because it should be:</p> <pre><code>printf("the data that should be at index 0 -&gt; %d\n", myContainer-&gt;P[0].array[0]); </code></pre> <p><code>myContainer-&gt;P</code> accesses a <strong>pointer</strong> to a <code>struct intArray</code>. The code right above means you access the first element (element number 0) of the array of <code>struct intArray</code>'s . </p> <p>To initialize/access the second element of the array of <code>struct intArray</code>'s use:</p> <pre><code>Initializer(&amp;(myContainter-&gt;P[1]), 5); printf("the data that should be at index 0 -&gt; %d\n", myContainer-&gt;P[1].array[0]); </code></pre>
    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.
 

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