我正在尝试读取文件,将值分配给结构成员,然后将值添加到动态内存分配中 . 我使用for循环读取所有值,然后将它们添加到动态数组中 . 它在编译时给了我错误 .
如何将值分配给Structure成员,同时将它们添加到动态数组?
我真的很感谢你的帮助!!谢谢 .
这是我的代码
struct Student
{
char *name[20];
char *gender[10];
int age;
char *course[20];
};
struct elements
{
struct Student *s;
struct elements *next;
};
FILE *file;
char c;
char type[30];
int main(int args, char *arg[])
{
struct Student *school = (struct Student *)malloc(sizeof(struct Student)); // Dynamic array
file = fopen(arg[1], "r"); // reading file
if(file != NULL)
{
while((c = fgetc(file)) != EOF) putchar(c);
{
int size;
int r;
for (r=0; r
{
fscanf("Name: %s", school->name);
//school->name = Addstudent(); // didn't work
//printf("names are: %s\n",school->name); // here I want to make sure the value name is in the array
}
}
}
else
{
printf("Unable to open file \n");
}
free(file);
return 0;
}
char* Addstudent()
{
char* name;
fscanf(file, "Name: %s",type ,name);
return name;
}
这是错误
在函数'main'中:Latest1.c:59:4:警告:从不兼容的指针类型传递'fscanf'的参数1 [默认启用] /usr/include/stdio.h:449:12:注意:预期' struct FILE * restrict'但是参数的类型为'char *'Latest1.c:59:4:警告:从不兼容的指针类型[默认启用]传递'fscanf'的参数2 /usr/include/stdio.h:449 :12:注意:预期'const char * restrict'但参数类型为'char **'Latest1.c:59:4:警告:格式不是字符串文字而且没有格式参数[-Wformat-security]