2

Is there possible to make the code work without changing the main function? I have tried using strcpy() in the push function but it results in segmentation fault (core dumped).
This is the output currently:

ccc ccc ccc
ccc ccc ccc
ccc ccc ccc

The output should be

ccc ccc ccc
bbb bbb bbb
aaa aaa aaa  

Code:

struct Node
    {
        char *data1;
        char *data2;
        char *data3;
        struct Node *next;
    };

   int main()
   {
       struct Node *head = NULL;
       char strings[3][10];
       char *s = "aaa";
       for (int i = 0; i < 3; i++)
       {
           strcpy(strings[i], s);
       }
       push(&head, strings);
       s = "bbb";
       for (int i = 0; i < 3; i++)
       {
           strcpy(strings[i], s);
       }
       push(&head, strings);
       s = "ccc";
       for (int i = 0; i < 3; i++)
       {
           strcpy(strings[i], s);
       }
       push(&head, strings);
   
       printList(head);
       freeList(&head);
   }

void push(struct Node **head_ref, char new_data[3][10])
{
   /* 1. allocate node */
   struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));

   /* 2. put in the data  */
   new_node->data1 = new_data[0];
   new_node->data2 = new_data[1];
   new_node->data3 = new_data[2];

   /* 3. Make next of new node as head */
   new_node->next = (*head_ref);

   /* 4. move the head to point to the new node */
   (*head_ref) = new_node;
}
4
  • 1
    Please post a minimal reproducible example. Also don't post pictures of text but post text as text. Commented Aug 26, 2020 at 11:54
  • 1
    "I have tried using strcpy in the push function". That would be more correct than what you have. What you have at the moment points all the nodes to the same strings so of course the result is only the last values copied into strings. Show your strcpy version if you want help with that. My guess is that you are not allocating memory for the dataX fields before calling strcpy. Commented Aug 26, 2020 at 11:56
  • 1
    Your structure contains pointers to strings. All three structs pushed into the stack contain the exact same pointers, that is pointers to strings. Commented Aug 26, 2020 at 11:59
  • ... or you might want this: char *data1 -> char data1[100] etc. in the struct definition and new_node->data1 = new_data[0] -> strcpy(new_node->data1, new_data[0]) etc. Commented Aug 26, 2020 at 12:01

2 Answers 2

3

You are using the same strings over and over. You must allocate (in push) new arrays for each string. For example:

new_node->data1 = malloc(10);
new_node->data2 = malloc(10);
new_node->data3 = malloc(10);

strcpy(new_node->data1, new_data[0]);
strcpy(new_node->data2, new_data[1]);
strcpy(new_node->data3, new_data[2]);
Sign up to request clarification or add additional context in comments.

3 Comments

OK. Please click the check mark on the left of my solution to indicate it helped you.
Just wonder do I need to loop through the data1,data2, data3 to free the list?
@wendy - Yes, for each call to malloc(), there should be a corresponding call to free().
1

The problem in your code is that the pointers of the allocated nodes are all pointing to the same 3 memory locations: strings[0], strings[1], and strings[2].

The statements:

new_node->data1 = new_data[0];
new_node->data2 = new_data[1];
new_node->data3 = new_data[2];

do not copy the strings to data1, data2, and data3 but rather store the memory addresses in them.

If you want to copy the strings contained in new_data[0], new_data[1], and new_data[2], you should allocate memory to data1, data2, and data3 and use the function strcpy() to do it.

example:

new_node->data1 = malloc(strlen(new_data[0]) + 1);
new_node->data2 = malloc(strlen(new_data[1]) + 1);
new_node->data3 = malloc(strlen(new_data[2]) + 1);

strcpy(new_node->data1, new_data[0]);
strcpy(new_node->data2, new_data[1]);
strcpy(new_node->data3, new_data[2]);

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.