【发布时间】:2017-08-24 12:00:50
【问题描述】:
我将这个结构用于我的树:
typedef struct product{
char name[50];
char id[5];
double price;
int amount;
struct product *left_p, *right_p;
}product_t;
所以,我必须将树转换为数组。 我为树维度写了这个:
int tree_dim(product_t *node_p){
int i = 1 ;
if (node_p == NULL)
i = 0;
else{
i += tree_dim(node_p->left_p);
i += tree_dim(node_p->right_p);
}
return i;
}
我的树是通过从 txt 文件中读取记录来填充的。记录为 21,tree_dim 返回的值是正确的。该值存储在arr_dim。
然后我创建一个product_t *products_a;,它将成为“数组”并使用products_a = malloc (arr_dim*sizeof (product_t));在内存中分配它
现在,这是用树节点填充数组的函数:
void fill_array(int *index, product_t *node_p, product_t *products_a){
if (node_p != NULL){
fill_array(index, node_p->left_p, products_a);
products_a[*index++] = *node_p;
fill_array(index, node_p->right_p, products_a);
}
}
但它给了我分段错误错误,所以我也尝试了第二个解决方案:
int fill_array(product_t *node_p, product_t *products_a){
int i = 1 ;
if (node_p == NULL){
i=0;
}
else
{
i += fill_array(node_p->left_p, products_a);
products_a[i-1] = *node_p;
i += fill_array(node_p->right_p, products_a);
}
return i;
}
这不会给出分段错误,但是当我打印数组时有空位置。 我需要一些关于我错在哪里的提示。可能是索引和递归调用有问题,但我想不通。
【问题讨论】:
-
使用调试器似乎是了解您的问题的最佳方式。缺少对
products_a的溢出测试。 -
我认为
*index++没有达到您的预期。
标签: c arrays pointers segmentation-fault binary-tree