Je struct comme ceci:question simple pointeur imbriqué
struct Child
{
int foo;
char bar[42];
};
struct Parent
{
long foobar;
struct Child ** children;
size_t num_children;
}
J'ai défini une API comme ceci:
struct Parent * ParentAlloc() { struct Parent* ptr = (struct Parent*)calloc(1, sizeof(struct Parent));
ptr->children = (struct Child**)calloc(SOME_NUMBER, sizeof(struct Child*));
return ptr;
}
Maintenant, si je veux supprimer un (précédemment attribué) enfant - en supposant index n'est pas hors limites:
void FreeChild(struct Parent* parent, const size_t index)
{
free(parent->children[index]);
//now I want to mark the address pointed to in the array of pointers as null, to mark it as available
//I dont think I can do this (next line), since I have freed the pointer (its now "dangling")
parent->children[index] = 0; // this is not right. How do I set this 'freed' address to null ?
}
Merci - beaucoup plus propre que mon code d'origine (bien que je sois assuré que c'était correct, je me suis trompé parce que je mélangeais un tableau de ptrs et un tableau de structures). Merci à tous ceux qui ont répondu. Infiniment reconnaissant – scoobydoo