0

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;
    }
} 
2
  • 2
    You have a structure full of pointers for the strings, but where do you make these pointers actually point somewhere? Commented Aug 22, 2021 at 12:55
  • 1
    And not only that, the scanf function expects the arguments for %s to be char *. The argument &nn->firstname has the type char ** which is wrong and leads to undefined behavior. Same with lastname. Commented Aug 22, 2021 at 12:56

1 Answer 1

1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.