이 같은 구조체가 : 나는 (이전에 할당 된) 아이를 삭제하려면,간단한 중첩 된 포인터 질문
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;
}
지금 - 가정 :
struct Child
{
int foo;
char bar[42];
};
struct Parent
{
long foobar;
struct Child ** children;
size_t num_children;
}
내가 API를 다음과 같이 정의를 인덱스가 범위를 벗어되지 않습니다 :
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 ?
}
감사합니다. 원래의 코드보다 훨씬 깔끔합니다. (필자는 확신했지만, ptrs 배열과 struct 배열을 혼합했기 때문에 혼란스러워졌습니다.) 대답 한 모든 사람들에게 감사드립니다. 감사합니다 – scoobydoo