【发布时间】:2015-06-26 11:02:29
【问题描述】:
我在下面使用 fgets 有两个场景。两种方案都在同一个文件中,如下所示。
struct sth
{
char str[10];
int num;
};
void getIt(struct sth **M){
char *b;
(*M)=malloc(sizeof(struct sth));
printf("give me an integer:");
fgets(b,1,stdin); // output must be an address
(*M)->num = atoi(b);
printf("give me a string:");
fgets((*M)->str,10,stdin);
}
int main(int argc, char const *argv[])
{
struct sth *myThing;
getIt(&myThing);
printf("Heres the string %s\n", myThing->str);
printf("Heres the num \n", myThing->num);
return 0;
}
这是输出。请注意,它不会提示用户输入整数,它只是打印“给我一个整数”,然后直接转到下一个打印语句。为什么这样做?
give me an integer:give me a string:sdf
Heres the string sdf
Heres the num
这个小问题是大问题中的大问题,所以这只是大问题的一个缩影。
【问题讨论】: