链表字符串不在 C 中打印

Linked List strings not printing in C

我正在用 C 语言创建一个电话簿应用程序,在打印链表(名字和姓氏)中的字符串时遇到了问题,如显示功能所示。 'number' 整数正在打印,但字符串没有。非常感谢您的帮助。谢谢

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

struct node { 
    char *firstname; 
    char *lastname; 
    int *number; 
    struct node *next; 
}*head; 

struct node *start=NULL; 

struct node *getnode() { 
 return((struct node *)malloc(sizeof(struct node)));
}

void display() { 
    struct node *temp; 
    temp=start; 
    if(temp!=NULL) {  
        printf("%s \n", temp->firstname); 
        printf("%s \n", temp->lastname); 
        printf("%d \n", temp->number); 
        temp=temp->next; 
    } else {
        printf("Please create an entry\n"); 
    }
} 

void insert() { 
    struct node *temp,*nn; 
    nn=getnode(); 
    temp=start; 
    while(temp->next!=NULL) 
    { 
        temp=temp->next; 
    } 
        printf("Enter First name:\n"); 
        scanf("%s",&nn->firstname); 
        printf("Enter Last name:\n"); 
        scanf("%s",&nn->lastname); 
        printf("Enter number:\n"); 
        scanf("%d",&nn->number); 
        temp->next=nn; 
        nn->next=NULL; 
        display(start);
} 

struct node *create() {
    struct node *temp,*nn; 
    if(start!=NULL) insert(); 
    else { 
        nn=getnode(); 
        start=nn; 
        temp=start; 
        printf("Enter First name:\n"); 
        scanf("%s",&nn->firstname); 
        printf("Enter Last name:\n"); 
        scanf("%s",&nn->lastname); 
        printf("Enter number:\n"); 
        scanf("%d",&nn->number); 
        nn->next=NULL;
    }
} 

您的节点仅包含指向 'string' (char*) 的指针,但没有为该输入字符串分配内存。 您有 2 个选择:

一个解决方案是将 firstname 和 lastname 更改为数组而不是指针,这样每次为节点分配内存时,您也分配了足够的内存来存储字符串(请注意,我还删除了 int 指针)。

您还需要删除 scanf 函数中名字和姓氏的 &。

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

struct node {
    char firstname[32];
    char lastname[32];
    int number;
    struct node *next;
}*head;

struct node *start = NULL;

struct node *getnode() {
    return((struct node *)malloc(sizeof(struct node)));
}

void display() {
    struct node *temp;
    temp = start;
    if (temp != NULL) {
        printf("%s \n", temp->firstname);
        printf("%s \n", temp->lastname);
        printf("%d \n", temp->number);
        temp = temp->next;
    }
    else {
        printf("Please create an entry\n");
    }
}

void insert() {
    struct node *temp, *nn;
    nn = getnode();
    temp = start;
    while (temp->next != NULL)
    {
        temp = temp->next;
    }
    printf("Enter First name:\n");
    scanf("%s", nn->firstname);
    printf("Enter Last name:\n");
    scanf("%s", nn->lastname);
    printf("Enter number:\n");
    scanf("%d", &nn->number);
    temp->next = nn;
    nn->next = NULL;
    display(start);
}

struct node *create() {
    struct node *temp, *nn;
    if (start != NULL) insert();
    else {
        nn = getnode();
        start = nn;
        temp = start;
        printf("Enter First name:\n");
        scanf("%s", nn->firstname);
        printf("Enter Last name:\n");
        scanf("%s", nn->lastname);
        printf("Enter number:\n");
        scanf("%d", &nn->number);
        nn->next = NULL;
    }
}

另一种选择是在创建新节点时为字符串动态分配内存。 请注意,在此解决方案中,您还需要在使用结束时释放此内存(它不会在您释放父节点时自动释放)。