繁体   English   中英

在C中的结构双向链表中写入字符串

[英]writing to strings inside a structure doubly linked list in C

我的代码有问题,我更改了一些函数以适应我添加的结构,因此不是到处都是变量,但现在根本不起作用。 我需要它来创建一个结构体人员,然后提示用户输入人员名称和年龄。 然后它要求更多人填写双向链接列表,如果没有输入任何人名,则该循环将停止循环。 然后,它吐出与我输入的双向链表相反的内容。 感谢所有帮助^-^

struct person
{
    char name[10][41];
    int age[10];
};

int write(struct person *people);
void print(struct person *people);
int main(void)
{
    char names[10][41];
    int n = 10;
    int ages[10];

    typedef struct person people;

    n = write(people);
    print(people);


    system("PAUSE");
    return 0;
}


int write(struct person *people)
{
    int i;
    char name[41];
    int age[10];
    for(i=0; i<=i; i++)
    {
        fflush(stdin);
        printf("Enter full name\n");

        gets(people.name);
        strcpy(names[i], name);

        if(names[i][0] == '\0')
            break;

        printf("Enter their age\n");
        scanf("%d", &age[i]);
        ages[i] = age[i];
    }
}

void print(struct person *people)
{
    int i = 0;
    for(i = 0; i < 10; i++)
    {
        if(names[i][0] == '\0')
            break;

        printf("%s is %d year(s) old\n", names[i], ages[i]);
    }
    return i;
}
  1. 您传递的是刚定义的类型的名称,而不是该类型声明的变量,

     typedef struct person people; 

    应该

     struct person people; 
  2. 更改

     n = write(people); 

     n = write(&people); 
  3. 删除fflush(stdin)是未定义的行为。

  4. 不要使用gets()非常不安全,请改用fgets()

     char name[40]; gets(name); 

    应该

     char name[40]; fgets(name, sizeof(name), stdin); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM