Greetings all; what I'm trying to do is create an array of structs dynamically by using functions for allocation, display, and then freeing memory. I'm *able* to do the process if it's within scope of the foo declaration, but the initFoo function throws an exception. I *think* it's the order of dimensions, but I don't know how to correct it.
struct declaration
typedef struct {
char** _2darray;
} foo;
the declaration
foo *bar = NULL; /* want to create an array of 5 */
initFoo(bar);
...
the definition
void initFoo(foo *tmp) {
/* malloc returns are omitted for space */
int i=0, j=0;
tmp = malloc(sizeof(*tmp) * 5);
while(i < 5) {
/* loop through each of the 5 elements */
/* create 5 strings for each element */
tmp[i]._2darray = malloc(sizeof(*tmp[i]._2darray) * 5);
while(j < 5) {
/* create space for each string */
tmp[i]._2darray[j] = malloc(20);
sprintf(tmp[i]._2darray[j], "testing string %d, line %d", i+1, j+1);
++j;
}
++i;
}
}
I don't quite get how to properly do this within a function and could use help. Thank you for your time in advance.