fgets()的行为,用于从stdin读取输入

时间:2018-08-08 11:54:35

标签: c fgets

我正在使用fgets从控制台读取字符串,但是在下面的程序中,当我使用fgets接受for循环内的输入时,在第一次迭代中,不会读取任何输入,并且将打印空白,并且仅从循环fgets接受我在哪里做错的输入?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student
{
    char *name;
};

void main()
{
    struct student *stud;
    int n,i;

    printf("Enter n value: ");
    scanf("%d",&n);

    stud = (struct student *)malloc(sizeof(struct student)*n);

    for(i=0;i<n;i++)
    {
        (stud+i)->name = (char *)malloc(sizeof(char)*25);
        printf("Enter name \n");
        fgets((stud+i)->name,25,stdin);

        printf("%s",(stud+i)->name);

    }
}

0 个答案:

没有答案