当我输入名称时,此代码将进入循环,但不会出现。有什么问题?
# define M 3
struct clas
{
char name[16];
int key;
};
struct node
{
int n;
struct node *p[M];
struct clas clsf[M-1] ;
};
main()
{
int i;
struct clas clsf;
for(i=0;i<2;i++)
{
root = malloc(sizeof (struct node));
printf("enter name \n");
scanf("%s\n",root->clsf[i].name);
printf("%s\n",root->clsf[i].name);
printf("enter key\n");
scanf("%d", &root->clsf[i].key);
printf("%d",root->clsf[i].key);
}
}
最佳答案
root
未定义,并且有一个未定义的struct classifier
实例:clsf
。将
struct classifier clsf;
替换为struct node *root;
从
scanf
格式中删除结尾的\n
:rpelace
scanf("%s\n",root->clsf[i].name);
和scanf("%s",root->clsf[i].名称);
关于c - 输入数据到结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849172/