从您的评论中,听起来您想要一个指向结构指针数组的指针,而不是指向结构数组的指针,因为“指向 char * 数组的指针”也有两个间接级别。
区别如下:
指向结构的指针:
指向结构数组的指针:
指向结构指针数组的指针:
假设您想要#3,您可以这样做(在“传统”C 中):
typedef struct myStruct myStruct;
struct myStruct
{
int size;
char *name;
myStruct **array;
};
myStruct *allocate_node(char *name, int size)
{
myStruct *p_node;
if (size < 0)
size = 0;
p_node = calloc(1, sizeof(myStruct));
p_node->name = name;
p_node->size = size;
p_node->array = calloc(1, size * sizeof(myStruct *));
return p_node;
}
void expand_node_child_array(myStruct *p_node, int size_to_add)
{
if (size_to_add < 1 || p_node == NULL)
return;
if (p_node->array == NULL)
{
p_node->size = size_to_add;
p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
}
else
{
p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
memset(p_node->array + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
p_node->size += size_to_add;
}
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return 0;
return p_node->array[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
p_node->array[index] = p_child;
return TRUE;
}
void free_node(myStruct **pp_node)
{
// Free p_node and the array but DO NOT free the children
if (pp_node == NULL || *pp_node == NULL)
return;
if ((*pp_node)->array != NULL)
free((*pp_node)->array);
free((*pp_node));
*pp_node = NULL;
}
void free_node_and_children(myStruct **pp_node)
{
int iChild;
if (pp_node == NULL || *pp_node == NULL)
return;
for (iChild = 0; iChild < (*pp_node)->size; iChild++)
{
myStruct *p_child = get_child_node((*pp_node), iChild);
if (p_child != NULL)
free_node_and_children(&p_child);
set_child_node((*pp_node), iChild, NULL);
}
free_node(pp_node);
}
更新
在 C99 标准语法下,灵活数组是一个可变长度数组,出现在结构的尾部,其实际长度在运行时设置。它在内存中看起来像这样:
假设您的编译器支持这种语法(并非全部支持),您可以像这样声明它:
struct myStruct
{
Type array_of_type[]; /* AT THE END OF THE STRUCT ONLY */
};
“myStruct”的代码变为:
typedef struct myStruct myStruct;
struct myStruct
{
int size;
char *name;
myStruct *array[];
};
myStruct *allocate_node(char *name, int size)
{
myStruct *p_node;
if (size < 0)
size = 0;
p_node = calloc(1, sizeof(myStruct) + size * sizeof(myStruct *));
p_node->name = name;
p_node->size = size;
return p_node;
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return NULL;
return p_node->array[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
p_node->array[index] = p_child;
return TRUE;
}
void free_node(myStruct **pp_node)
{
if (pp_node == NULL || *pp_node == NULL)
return;
free((*pp_node));
*pp_node = NULL;
}
void free_node_and_children(myStruct **pp_node)
{
int iChild;
if (pp_node == NULL || *pp_node == NULL)
return;
for (iChild = 0; iChild < (*pp_node)->size; iChild++)
{
myStruct *p_child = get_child_node((*pp_node), iChild);
if (p_child != NULL)
free_node_and_children(&p_child);
set_child_node((*pp_node), iChild, NULL);
}
free_node(pp_node);
}
如果您的编译器没有,请参阅此处了解一些解决方法。
使用灵活的数组,扩展数组需要重新分配节点本身并修复对它的所有引用,这在“指针数组的指针”设计中不需要。
您正在使用的语法:
myStruct *(*array)[];
应该读作“指向结构指针大小未知的数组的指针”,而不是
myStruct **array;
这是“指向结构的指针的指针”,或(例如):
myStruct *(*array)[4];
这是“指向长度为 4 的指针数组的指针。
您的语法实际上会生成内存映射 #3,但是访问单个元素有点尴尬,因为您必须显式获取指向“未知大小数组”的第零个元素的指针,即(*p_node->array)
. 因此,#3 中的函数修改如下:
void expand_node_child_array(myStruct *p_node, int size_to_add)
{
if (size_to_add < 1 || p_node == NULL)
return;
if (p_node->array == NULL)
{
p_node->size = size_to_add;
p_node->array = calloc(1, size_to_add * sizeof(myStruct *));
}
else
{
p_node->array = realloc(p_node->array, (p_node->size + size_to_add) * sizeof(myStruct *));
memset((*p_node->array) + p_node->size * sizeof(myStruct *), 0, size_to_add * sizeof(myStruct *));
p_node->size += size_to_add;
}
}
myStruct *get_child_node(myStruct *p_node, int index)
{
if (index < 0 || index >= p_node->size)
return NULL;
return (*p_node->array)[index];
}
int set_child_node(myStruct *p_node, int index, myStruct *p_child)
{
if (index < 0 || index >= p_node->size)
return FALSE;
(*p_node->array)[index] = p_child;
return TRUE;
}
最后,两种架构的测试代码:
void dump_nodes_recursive(myStruct *p_node, int level)
{
if (p_node == NULL)
{
printf("%*s", 4*level, " ");
printf("NULL\n");
}
else
{
int iChild;
printf("%*s", 4*level, " ");
printf("Node: Name=\"%s\", array size=%d\n", p_node->name, p_node->size);
for (iChild = 0; iChild < p_node->size; iChild++)
{
myStruct *p_child = get_child_node(p_node, iChild);
printf("%*s", 4*level, " ");
printf("Child [%d]:\n", iChild);
dump_nodes_recursive(p_child, level+1);
}
}
}
void dump_nodes(myStruct *p_node)
{
dump_nodes_recursive(p_node, 0);
}
void test_my_struct()
{
myStruct *p_top = allocate_node("top", 4);
myStruct *p_child0 = allocate_node("child0", 1);
myStruct *p_child1 = allocate_node("child1", 5);
myStruct *p_child2 = allocate_node("child2", 0);
myStruct *p_child3 = allocate_node("child3", 0);
myStruct *p_child00 = allocate_node("child00", 0);
set_child_node(p_top, 0, p_child0);
set_child_node(p_top, 1, p_child1);
set_child_node(p_top, 2, p_child2);
set_child_node(p_top, 3, p_child3);
set_child_node(p_child0, 0, p_child00);
dump_nodes(p_top);
free_node_and_children(&p_top);
}