Omer Guttman
Omer Guttman

Reputation: 141

Understanding linked lists in C

I have recently started to learn about linked lists and I was wondering 2 things about my code:

  1. Why does the program crash?

  2. Why does it type every single member with a newline \n at the end?

This is my code. All I was trying to do is add one member and print it.

struct node
{
    char* day;
    char* month;
    char* year;
    struct node* next;
};
typedef struct node node;

void print(node* head){
    node* temp=head;
    while(temp != NULL){
    printf("%s %s %s",temp->day,temp->month,temp->year);
    temp=temp->next;
    }
}

node* add(node* head)
{
    node * new_node;
    int n;
    char temp[25];
    new_node = malloc(sizeof(node));

    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->day=malloc(n+1);
    strcpy(new_node->day,temp);

    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->month=malloc(n+1);
    strcpy(new_node->month,temp);

    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->year=malloc(n+1);
    strcpy(new_node->year,temp);

    new_node->next=head;
    return new_node;
}

int main(void)
{
    node* head=NULL;
    head=malloc(sizeof(node));
    if (head == NULL)
    {
        return 1;
    }
    head=add(head); 
    print(head);
    return 100;
}

Can anyone point out what I am doing wrong and what I could have done better?

Upvotes: 1

Views: 87

Answers (2)

EsmaeelE
EsmaeelE

Reputation: 2658

I change your code to :

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

struct node
{
    char* day;
    char* month;
    char* year;
    struct node* next;
};
typedef struct node node;

void print(node *head){
    node *temp = head;

    while(temp){
        printf("day:%s\nmonth:%s\nyear:%s\n",temp->day,temp->month,temp->year);
        temp=temp->next;
    }

}
node* add(node* head)
{
    node * new_node;
    int n;
    char temp[25];
    new_node = malloc(sizeof(node));
    printf("day:\n");
    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->day=malloc(n+1);
    strcpy(new_node->day,temp);

    printf("Month:\n");
    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->month=malloc(n+1);
    strcpy(new_node->month,temp);
    printf("Year:\n");
    fgets(temp,sizeof(temp),stdin);
    n=strlen(temp);
    new_node->year=malloc(n+1);
    strcpy(new_node->year,temp);

   /// new_node->next=head;///

    printf("\n\n\n");
    //printf("%s%s%s \n",new_node->day,new_node->month,new_node->year);

    return new_node;
}
int main(int argc,char* argv[])
{

    node* head=NULL;
    head=malloc(sizeof(node));
    if (head == NULL)
    {
        printf("NULL\n");///cant allocate memory
    }
    printf("this\n");
    head=add(head); 
    print(head);
    return 100;
}

Upvotes: 0

Simone Cifani
Simone Cifani

Reputation: 784

When you do a

ptr = malloc(sizeof(node)); 

the memory is allocated but not initialized. This will cause the next pointer of the node struct to point to something undefined. Then, when you use it in the print function, the program crashes.
Put a

memset(ptr, 0, sizeof(node)) 

after the malloc or explicitly initialize the next pointer to NULL.

Upvotes: 2

Related Questions