C创建节点以包含来自用户输入的名称和年龄



我正在尝试创建一个字符串和整数的链表,如这里所示的name&年龄到目前为止效果不佳!如果有人能解释我的错误在哪里以及如何改正!!

#include <stdlib.h>
#include <stdio.h>
struct node
{
char* name;
int value;
struct node *next;
};
int main(void)
{
// Creating a Node to store the data.
struct node* Node = malloc(sizeof(struct node));
//Ask the user to enter a Name.
printf("Enter name: ");
//scanning the input and store it into the Node name part.
scanf("%sn", Node-> name);
//ask the user to enter age.
printf("Enter age:");
//scanning the input and store it into the Node age part.
scanf("%dn", &Node-> value);

printf(" Name:%s , Age:%dn %pn  ",Node -> name ,Node -> value, &Node);
}
  1. 您没有为name创建空间
  2. 不要在scanf上使用n
  3. 我不知道为什么要打印指针,但不需要使用&Node,因为它已经是一个指针了
#include <stdlib.h>
#include <stdio.h>
struct node
{
char* name;
int value;
struct node *next;
};
int main(void)
{
// Creating a Node to store the data.
struct node* Node = malloc(sizeof(struct node));
//Ask the user to enter a Name.
printf("Enter name: ");
Node->name = malloc(sizeof (char) * 100); // max 100 chars
//scanning the input and store it into the Node name part.
scanf("%s", Node-> name);
//ask the user to enter age.
printf("Enter age: ");
//scanning the input and store it into the Node age part.
scanf("%d", &Node-> value);
printf(" Name:%s , Age:%dn %pn",Node -> name ,Node -> value, Node);
return 0;
}

相关内容

  • 没有找到相关文章

最新更新