I have an array of pointers to structs and I'm trying to find a way to fill the first NULL pointer in an array with a new pointer to a struct. i.e. I want to add a new element onto the end of an array. I tried a for loop like this:
struct **structs;
int i;
for(i = 0; i < no_of_pointers; i++) {
if (structs[i] == NULL) {
structs[i] = &struct;
}
}
In theory, this would go through the array and when it finds a null pointer it would initialise it. I realise now that it would initialise all null pointers, not just the first, but when I run it it doesn't even do that. I've tried a while loop with the condition while(structs[i] != NULL) and that just goes on forever, making me think that the issue is with how I'm using NULL. What is the correct way to add a new element to an array of this kind? Is there some function like append(structs, struct) that I don't know of? Thanks!
structsbefore this? You have initialized each pointer in the "array" toNULL? If you're usingmallocit will not initialize the memory it allocate for you.structsandno_of_pointers?structis a keyword and cannot be used as a variable name. This means we aren't looking at your real code, which means there may be other extra errors in the code. It's best, in general, to show an extract from your real code rather than to misparaphrase it.