我正在尝试创建一个字符串和整数的链表,如这里所示的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);
}
- 您没有为
name
创建空间 - 不要在
scanf
上使用n
- 我不知道为什么要打印指针,但不需要使用
&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;
}