I have a simple structure like
typedef struct myPtr myPtr;
typedef struct example {
myPtr* list_ptr;//just a list of pointers, doesnt matter for now
int nb_elements;
int nb_mllc;
} example;
and in my c file example.c i have implemented all functions needed for this, to malloc, realloc, free and add entries...
example* create_example(int num){
example* ex = malloc(sizeof(example));
if (ex == NULL) //do some error catching
ex->list_ptr = malloc(sizeof(myPtr) * num);
if (ex->list_ptr == NULL) //do some error catching
ex->nb_mllc = num;
ex->nb_elements = 0;
return ex;
}
Now if I want to iterate over the entries of the structure, I know I can simply do it by using a for loop
example* ex = create_example(10);
//lets assume the entries are filled, so
// ex->nb_elements is greater than 0
for (int i=0; i<ex->nb_elements; i++) //... do stuff
However I was wondering if it is possible to write a function, such that I iterate over the entries without knowing anything about the structure. So I am in another c file "another_file.c" and I want simply to not care about the structure itself, so just create it and then call a function like iterate_over, so skipping writing this loop, since then I also need to know how the structure variables look like in my "another_file.c" file. So something like
example* ex = create_example(10);
iterate_over(ex); //but then I dont know how to access the current element from here
//or do something like
while(iterate_over(ex) != NULL) //saying it is empty..
Can someone give me a hint how to do this, as I dont know how can I hide all the indexes and information needed here..