Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can of course do it. Not nice, not useful, but can be done by manipulating your pointer.</p> <p>To understand, look at the layout of your array of structures in memory, let's say your array start at address 0x0000 :</p> <pre><code>0x0000 : array[0].first - first part of mys.first 0x0004 : array[0].first - second part of mys.first 0x0008 : array[0].second 0x000c : array[0].str[0-3] - first four elements of your string 0x0010 : array[0].str[4-7] - next four elements 0x0014 : array[0].str[8-9] - last two elements and padding 0x0018 : array[1].first - second element ! </code></pre> <p>...</p> <p>So you can see that between the same field of two consecutive elements of the array you have the size of the structure.</p> <p>To iterate over the same field of several consecutive structures in memory you then just need to declare a pointer pointing to the element of the first structure and increase it by the size of your structure to access the same field of next element of the array.</p> <p>In your case, it would then look like : </p> <pre class="lang-c prettyprint-override"><code>int i; struct mys my_array[SIZE]; char* my_iterator = &amp;my_array[0].second; for(i=0; i&lt;SIZE; i++) { fprintf(stdout, "second-&gt;%lu\n", *(unsigned long*)my_iterator); my_iterator += sizeof(mys); } </code></pre> <p>But this really doesn't look good, you can improve it a bit, but still it's not good practice to play with pointers when it's not needed.</p> <pre class="lang-c prettyprint-override"><code>int i; struct mys my_array[SIZE]; strut mys* my_iterator = &amp;my_array[0].second; for(i=0; i&lt;SIZE; i++, my_iterator++) { fprintf(stdout, "second-&gt;%lu\n", *(unsigned long*)my_iterator); } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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