1

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?

5
  • 4
    What did you see when you used a debugger to analyze your code's behavior? Commented Apr 26, 2017 at 17:40
  • I suggest that you use a debugger or add printf() statements to find which line in your code causes the crash. Learning to debug code is just as important as learning to write code. Commented Apr 26, 2017 at 17:40
  • the problem is the print in the row of printf function, im guessing its trying to reach and print a something without value but i dont really understand how Commented Apr 26, 2017 at 17:42
  • (1) You are using an uninitialized node. So Remove head=malloc(sizeof(node)); if (head == NULL) { return 1; } Commented Apr 26, 2017 at 17:48
  • 1
    (2) result of fgets contains entered newline. Commented Apr 26, 2017 at 17:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

There isn't very much explanation of what you changed and why you changed it. Almost always, a good answer will provide an explanation of the changes as well as the changed code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.