向用户询问数据并将它们存储在 C 中的结构中

Ask data from user and store them inside a struct in C

在下面的代码中,我尝试使用 printf 和 scanf 从用户获取数据并将它们存储在我定义的名为节点的结构中。

程序在第一次提示时运行良好,但一旦用户输入名称,程序就会以打印结束 Age:salary:

谁能帮我解决这个问题?

顺便说一句,谁也能帮我理解如何创建一个循环来将数据存储在各个节点中并将它们存储在一起? (不是一一硬编码)

非常感谢!!

typedef struct node
{
    char *name;
    int age;
    int salary;
    struct node * next;
}node;

int main(void)
{
        node *tmp = malloc(sizeof(node));
        
        printf("Name:");
        scanf("%s", tmp->name);
        
        printf("Age:");
        scanf("%i", &(tmp->age));
        
        printf("salary:");
        scanf("%i", &(tmp->salary));
        
        tmp->next = NULL;
        
        free(tmp);
}

名称使用字符[], 例如:

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

typedef struct node
{
    char          name[128];
    int           age;
    int           salary;
    struct node * next;
}node;

int main(void)
{
    node *tmp = malloc(sizeof(node));

    printf("Name:");
    scanf("%s", tmp->name);

    printf("Age:");
    scanf("%i", &(tmp->age));

    printf("salary:");
    scanf("%i", &(tmp->salary));

    tmp->next = NULL;

    free(tmp);
}

如果你想获得多个用户,就一直循环并询问用户是否要添加更多数据。 我已经创建了一个函数来打印列表

控制台:

Name:Foo
Age:12
salary:12
Continue Y/N
Y
Name:Bar
Age:14
salary:14
Continue Y/N
Y
Name:John 
Age:30
salary:45
Continue Y/N
N
John, 30, 45
Bar, 14, 14
Foo, 12, 12
#include <stdlib.h>
#include <stdio.h>

typedef struct node
{
    char          name[128];
    int           age;
    int           salary;
    struct node * next;
}node;

static void printList(node *n)
{
    while (n) {
        printf("%s, %d, %d\n", n->name, n->age, n->salary);
        n = n->next;
    }
}

static node *get_nodes(void) {
    node *list = NULL;
    while (42) {
        char c;
        node *tmp = malloc(sizeof(node));

        printf("Name:");
        scanf("%s", tmp->name);

        printf("Age:");
        scanf("%i", &(tmp->age));

        printf("salary:");
        scanf("%i", &(tmp->salary));

        tmp->next = list;
        list = tmp;
        printf("Continue Y/N\n");
        scanf(" %c", &c);
        if (c == 'N')
            break;
    }
    return list;
}

static void clearList(node *node) {
    if (node->next) {
        clearList(node->next);
    }
    free(node);
}

int main(void)
{
    node *list = get_nodes();
    printList(list);
    clearList(list);
    return 0;
}

为了修复你的错误,你应该为你的 char *.

分配内存

第一种方式,创建结构时。

typedef struct node
{
    char name[200]; // You specify that your char * can save 200 char
    int age;
    int salary;
    struct node * next;
}node;

或者您可以创建 init struct 函数

node *init_node()
{
   node *test = null;
   test->name = malloc(sizeof(char) * 200);
   test->age = 0;
   test->salary = 0;
   test->node = null;
   return test
}

您正在尝试写入未初始化的内存

这是初学者面临的一个非常普遍的问题。

您正在尝试使用 char *name 声明来存储名称。 这里的名称没有指向一个有效的内存位置,这就是为什么你的程序不是预期的运行。

即使 name 指向有效的内存地址,您也必须分配足够的内存来存储数据。

您可以使用

#define BUFFER_SIZE 50
char name[BUFFER_SIZE];

您可以根据需要使用任何缓冲区大小,然后将长度为 - 1 的字符串存储在 name 数组中。 -1 用于 null termination character [=16=].

使用此声明,您将分配 BUFFER_SIZE 字节的内存,并且 name 指向该数组中的第一个字节。

这个分配发生在栈上而不是堆