提问人:Gostodexadrez 提问时间:5/31/2023 更新时间:5/31/2023 访问量:30
为什么 scanf 不适用于使用指针和 malloc 的 C 程序中的整数变量?[复制]
Why does scanf not work for the integer variables in my C program using pointers and malloc? [duplicate]
问:
使用指针和 malloc 扫描结构的一部分时出现问题
#include <stdio.h>
#include <stdlib.h>
struct evento{
char nome [100];
char local [100];
int dia;
int mos;
int anos;
};
int main (){
int i,n;
i=0;
scanf ("%d",&n);
struct evento *agenda;
agenda =(struct evento*) malloc(n*sizeof(struct evento));
for (i=0;i<n;i++){
fgets (agenda[i].nome,100,stdin);
fgets (agenda[i].local,100,stdin);
scanf ("%d",&agenda[i].dia);
scanf ("%d",&agenda[i].mos);
scanf ("%d",&agenda[i].anos);
}
printf ("happy day");
free(agenda);
return 0;
}
当我输入 n 和两个字符串的值时,程序将打印引号,而不是让我输入 dia、mos 或 anos 的值。
因此,如果我输入类似 1(n 的值)、名称和事件之类的内容,程序将完成并打印引号,而不是让我输入其他值。为什么???
答: 暂无答案
评论
"hello\n"
world\n"
1
2
3