Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One quick hint about pointers: if you are casting it, you are probably doing something wrong.</p> <p>As for your question. I am not sure what <code>item</code> is in your problem. In your first part you've already discovered how to acces a member in your array. You could simply use it:</p> <pre><code>void *get_item(Thing *c, int index) { return *(c-&gt;array + index); // or return c-&gt;array[index]; } </code></pre> <p>If you need the address of the pointer at index:</p> <pre><code>void **get_item_cell(Thing *c, int index) { return c-&gt;array + index; // or return &amp;c-&gt;array[index]; } </code></pre> <p>In the second part, you don't dereference the pointer (for + option), or take the address of array result, since it automatically dereferences it.</p> <hr> <p>EDIT: I think I now know what you want. You have a function similar to my second one above, but it is:</p> <pre><code>void *get_item_cell(Thing *c, int index) { return (void *)(c-&gt;array + index); } </code></pre> <p>You want to dereference the value returned from this function, and access the object. In that case, you can only use Option 4 safely. Since you don't have the index, you cannot move to any other cell (you don't know if you are at the end of the array, or at the beginning - so no additions or subtractions). You can only fix the mistake of the above function: cast to <code>void **</code>, and then dereference it: <code>*(void **)item</code>. This will give you a <code>void *</code>. If you want to access the object pointed from this cell, you need to cast that to the correct type as well: <code>some_object_ptr *obj = *(void**)item</code>.</p>
    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.
    1. This table or related slice is empty.
    1. 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