Search code examples
carraysdynamiciteratoradt

Iterator on dynamic array in a struct


How can I make an iterator over a dynamic array (in a struct)?


Solution

  • Your dynamic array, as represented by type struct _Lista, does not contain "elements of any type". At least not directly. It contains only and exactly elements of type void *. Pretty much everything you've written will work if you change type iterator accordingly, to a double pointer:

    typedef void **iterator;
    

    Do note, however, that this iterator implementation has a severe flaw: it does not carry any information about the bounds of the list. That may be OK if you ensure that there is a sentinel value after the last valid element (and maybe before the first, too), but otherwise you'll need a more complex data structure than just one pointer.