我在动态分配树中节点的字符串时遇到了一些问题。我已经在下面包含了我的节点结构以供参考。从C中的文件创建动态分配的字符串
struct node
{
char *string;
struct node *left;
struct node *right;
};
typedef struct node node;
我应该从文本文件中读取单词,然后将这些单词存储到树中。我可以存储已定义的char数组,例如char string [20],但不包含应该动态分配的字符串。
我只会发布我正在使用的代码来读取我的文件,并尝试创建动态分配的数组。我已经创建了文件指针并检查它不是NULL。每次我尝试运行该程序时,它都会崩溃,我是否需要尝试逐字读取字符?
//IN MAIN
node *p, *root ;
int i;
int u;
root = NULL;
char input[100];
while(fscanf(fp, "%s", &input) != EOF)
{
//Create the node to insert into the tree
p = (node *)malloc(sizeof(node));
p->left = p->right = NULL;
int p = strlen(input); //get the length of the read string
char *temp = (char*) malloc(sizeof(char)*p);
//malloc a dynamic string of only the length needed
strcpy(local, input);
strcpy(p->word,local);
insert(&root, p);
}
要完全清楚,我只希望关于我的代码逻辑的建议,只希望有人帮我指出了正确的方向。
注意,他们说[你不应该投的malloc'()的结果在'C](HTTP:// STA ckoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 – MikeCAT
@MikeCAT。我知道这是一个错误,但我也不确定如何修复它。 &是我通常如何存储变量的值,但对于char数组,应该只是fscanf(ifp,“%s”,input)呢?此外,对于第三个评论,我应该也许只是使用一个循环并分配char值来确保我也复制null零? – Ash
是的,应该是。 '&'用于检索指向对象(变量)的指针。你使用'&hoge'来检索指向'int hoge;'的指针。数组自动转换为指向数组第一个元素的指针,所以您不必使用'&'来检索传递给'fscanf()'的指针。 – MikeCAT