Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your function is expecting a pointer to the structure. This arguement can be an individual instance of that structure or it could be an element in the array of the give structure. Like</p> <pre><code>struct myStruct { int a, b; long cL, dL; char e; } struc1, struc2, record[20]; </code></pre> <p>and function's prototype will be</p> <pre><code>function( struct myStruct *ptr); </code></pre> <p>Now you can pass the structure to function:</p> <pre><code>function( &amp;struct1 ); // or function( &amp;record[ index] ); </code></pre> <p>Now your confusion arises because of the misconception that syntax <code>array[i]</code> can also be treated as a pointer like we can do with the name of the <code>array</code>. </p> <p><code>record</code> - name of the array- gives the address of the first member of the array, (pointers also point to memory addresses) hence it can be be passed to the function. But <code>record[index]</code>, it is different. <br/></p> <p>Actually, when we write <code>record[ index]</code> it gives us the value placed there which is not a pointer. Hence your function which is accepting a pointer, does not accept it.</p> <p>To make it acceptable to the function, you will have to pass the address of the elements of the array i.e</p> <pre><code>function( &amp;record[ index ] ); </code></pre> <p>Here <code>&amp;</code> operator gives the address of the elements of the array.</p> <p>Alternatively, you can also use:</p> <pre><code>function( record + index ); </code></pre> <p>Here, as we know <code>record</code> is the address of the first element, and when we add <code>index</code> in it, it gives the address of the respective element using pointer arithmetic.</p> <p>Hope it was helpful.</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. 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