I've been struggling with a simple task in C... (It's been a while.) I need to build a function that creates and resets an array of structs without using any memory allocation functions.
I originally designed it with malloc:
typedef struct {
int ..
int ..
} Branch;
Branch* createBranchList (int N)
{
Branch *List;
Branch reSet = {0}; // a zero'd Branch struct used for the resetting process
int i;
if(!(List=(Branch*)malloc(sizeof(Branch)*N))){
printf("Allocation error");
return NULL;
}
for(i=0; i<N; i++)
List[i] = reSet;
return List;
}
Now how can I do this without using memory allocation? Can I return a reference? I don't think so.
Thanks anyone for helping out.