1

I am having a problem on assigning char array after I create a node. I am having trouble on this function I created which it would make a new node and then assign character array into the created node. I don't have any problems when it comes to int but I can't seem to run when I switched to character array.

I get a run time error when I try to run my code.

Any help would be much appreciated.

Thank You!

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

#define MAX 100   
   
struct node{  
    char data[MAX]; 
    struct node *next;  
};      

typedef struct node* nodePtr;

void create(nodePtr head, char data[]);

int main()
{
    nodePtr head = NULL;
    char str[] = "HELLO";
    create(head, str);
    return 0;
}

void create(nodePtr head, char data[])
{
    if (head == NULL)
    {
                // empty list
        head = (nodePtr) malloc(sizeof(struct node));  // create root
        strcpy(head->data, data);
        head->next = NULL;       
    }        
    else
    {            // list not empty
        nodePtr current = head; // start at the beginning...
        
        while (current->next != NULL)
        {
            current = current->next; // walk to the end of the list
        }
            
        current->next = (nodePtr) malloc(sizeof(struct node));
        current = current->next;
        strcpy(current->data, data);
    }
}      

2
  • This is not a complete program. What is the type of the data member of struct node? You should show a minimal example that produces the runtime error, including the input and output. Commented Mar 30, 2022 at 4:04
  • Oh sorry. Wait I'll edit it Commented Mar 30, 2022 at 4:10

1 Answer 1

1

There is more than one problem with your program.

To begin with, when you add elements to a list that already has a head, are not initializing the next member. This will cause multiple list insertions to fail.

current->next = malloc(sizeof(struct node));
current = current->next;
strcpy(current->data, data);
current->next = NULL;              //<-- you forgot this

The other big issue is that you are also leaking your entire list, because you pass the head pointer by value into the function. When the list is empty, the function modifies this value but it's a copy so when the function returns, the value of head in main is still NULL.

There are two options. Either you change your function to expect a pointer to the head-pointer, or you use what's called a "dummy head".

Using a dummy head actually simplifies lists a lot. What this means is your head is actually an unused node whose sole responsibility is to store the actual head in its next-pointer:

struct node head = {};  // dummy head node
create(&head, str);

The above will never actually hit the head == NULL part of the create function because dummy heads guarantee the head is always a valid node. So if you set your list up that way, then your create function can be simplified.

If you don't want to do this, then you need to pass a pointer to your head pointer. That means your create function becomes:

void create(nodePtr *head, char data[])
{
    if (*head == NULL)
    {
        *head = malloc(sizeof(struct node));
        strcpy((*head)->data, data);
        (*head)->next = NULL;       
    }        
    else
    {
        nodePtr current = *head;
        while (current->next != NULL) current = current->next;
        current->next = malloc(sizeof(struct node));
        current = current->next;

        if (current != NULL)
        {
            strcpy(current->data, data);
            current->next = NULL;
        }
    }
}      

And you would invoke the above as:

nodePtr head = NULL;
create(&head, str);

One extra thing you might wish to do is make the function return the node that it created. The reason you might want to do this is that currently if you're inserting many items into the list you have to search to the end every time. If instead you pass the last node as the next head, then no searching is necessary:

nodePtr head = NULL;
nodePtr tail = head;
tail = create(head, "goodbye");
tail = create(tail, "cruel");
tail = create(tail, "world");

This would mean a small modification to your function, but I'm sure you can work that out.

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

Comments

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.