1
我想从文件创建列表。这是我的代码。动态分配内存列表中的字符串C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char str1[200];
char str2[200];
char str3[200];
struct node *next;
}*start=NULL;
int main(){
FILE *fp;
fp = fopen("file", "r");
while(!feof(fp)){
struct node *new_node,*current;
new_node=(struct node*)malloc(sizeof(struct node));
fscanf (fp,"%s %s %s",new_node->str1,new_node->str2,new_node->str3);
new_node->next=NULL;
if(start==NULL) {
start=new_node;
current=new_node;
}
else {
current->next=new_node;
current=new_node;
}
}
fclose(fp);
}
现在我想STR1,STR2,STR3是动态分配的,但如果我用这个代码,我有这些错误(重复成员STR1,STR2,STR3,预计“;”在年底申报清单,类型名称需要一个说明符或限定符)
struct node {
char *str1;
#ERROR
str1=(char*)malloc(sizeof(char*)*200);
char *str2;
#ERROR
str2=(char*)malloc(sizeof(char*)*200);
char *str3;
#ERROR
str3=(char*)malloc(sizeof(char*)*200);
struct node *next;
}*start=NULL;
我正在使用Xcode。
既不能分配内存也不能初始化结构声明中的任何结构变量。 –