I have a structure :
struct Node{
struct Node* pointer[0];
int num;
} *current=NULL;
and then in function I trying to create children to node
void AddChild(struct Node *node) {
uint8_t n=2; // number of add children
for(uint8_t i=n; i-->0;){
struct Node* leaf=(struct Node*)malloc(sizeof(struct Node)); //allocate memory to child
struct Node* tmp=(struct Node*)realloc(node->pointer,(++node->num)*sizeof(struct Node*)); //reallocate memory for dynamic array mistake
if (!tmp)
printf("Error in memory alocation\n");
node->pointer[node->num]=leaf;
}
}
The problem is that realloc give me error.
realloc(): invalid pointer:
So if it was c++ I can make a vector and just push back the element, but with c I need to reallocate the array of pointers. How can I reallocate the memory?
struct Node* pointer[0];-->struct Node **pointer;and Rewrite according to this change.node->pointeris not a pointer; furthermore, the pointer you pass toreallocmust be a pointer that was allocated withmalloc/realloc.