Here's a very basic example of what I'm trying to do (note that this segmentation faults)
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
int *bar;
} Foo;
Foo **fooPointers() {
Foo **test = (Foo**) malloc(sizeof(struct foo) * 3);
for(int i = 0; i < 3; i++) {
Foo *curr = *(test + i);
int *num = &i;
curr->bar = num;
}
return test;
}
int main() {
fooPointers();
return 0;
}
the goal is to create an array of pointers of Foo, give each element meaningful values, and then return the pointer array.
Is anyone able to point me in the right direction as to why this doesn't work and how I can accomplish this task?
barelements) point to a local variable in the function is not going to work well once the function returns. You'd be better off setting those pointers to NULL.