Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It looks like you have missed something when defining the iterator structure. Why does the iterator have a function pointer to an 'end' function that accepts an iterator?</p> <p>If you want it to be really generic, you could perhaps use this definition instead:</p> <pre><code>typedef struct __iterator { void * ptr_to_container; int (*end)(void *); } iterator; int end(iterator * it) { return it-&gt;end(it-&gt;ptr_to_container)); } </code></pre> <p>In the vector definition (and other data types), you can then define a function to create an iterator:</p> <pre><code>static int vector_end(vector * v) { /* implementation omittted */ } iterator * vector_create_iterator(vector * v) { iterator * it = malloc(sizeof(iterator)); it-&gt;ptr_to_container = v; it-&gt;end = vector_end; return it; } </code></pre> <p>However, the solution really depends on how the data structures are defined. In the above suggestion, it is up to each data structure to provide an implementation for how to traverse it. </p> <p>As an alternative, you could set up a generic data structure interface, like</p> <pre><code>typedef struct _container container; struct _container { int (*end)(container * c); }; </code></pre> <p>Then the vector implementation would "only" have to fill in this container structure:</p> <pre><code>typedef struct _vector { container c; /* other fields required by the vector */ } static int vector_end(container * c) { vector * v = (vector *) c; ... } container * create_vector() { vector * v = malloc(sizeof(vector)); v-&gt;c.end = vector_end; return v; } </code></pre> <p>...and the iterator could work with just the generic container:</p> <pre><code>typedef struct _iterator { container * c; /* other fields used by the iterator, such as current position */ } int end(iterator * it) { return it-&gt;c-&gt;end(it-&gt;c); } </code></pre> <p>From the code sample in the question, it looks almost like you have mixed up these two approaches :-)</p>
 

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