Luke Grech
Luke Grech

Reputation: 185

Linked List strings not printing in C

I am creating a telephone directory app in C and have encountered problems printing the strings in the linked list (firstname and lastname), as seen in the display function. The 'number' integer is printing but the strings are not. I would greatly appreciate your help. Thanks.

#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;
    }
} 

Upvotes: 0

Views: 130

Answers (1)

Elchanan shuky Shukrun
Elchanan shuky Shukrun

Reputation: 337

Your node includes only pointer to 'string' (char*), but no memory is allocated for this input string. You have 2 options:

One solution is to change firstname and lastname to be arrays instead of pointers so each time you allocate memory for a node, you also allocate enough memory to store the strings (Note that I also remove the int pointer).

You also need need to remove the & in the scanf function for firstname and lastname.

#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;
    }
}

Another option is to allocate memory dynamically for the strings as you create a new node. Note that in this solution you also need to free this memory at the end of your usage (it will NOT free automatically as you free the parent node).

Upvotes: 1

Related Questions