4

I'm trying to create a linked list with a head and a tail node in C. Each node needs to hold an int and a string. My issue is that when I create a new node, assign it the correct values, and add it to the end of the list. All previous nodes obtain the string that I assigned the newest node. The int values stay correct, but its like their char pointer gets reassigned. I'm guessing I'm doing somethign wrong with pointers and have looked at tons of examples online and can't see where I'm going wrong. I included my node structure and add function. Thanks for all the help!!

// Linked list (queue) for requests
typedef struct request {
    struct request *next;
    int request_id;
    char *command;
} request_t;

// Global variables
request_t *head = NULL;
request_t *tail = NULL;

void add(int r_id, char *c) {
    request_t *node = NULL;
    node = (request_t *)malloc(sizeof(request_t));

    node->request_id = r_id;
    node->command = c;
    node->next = NULL;

    if(head == NULL) {
        head = node;
        tail = node;
    } else {
        tail->next = node;
        tail = node;

    }   
}
3
  • You're error is not with pointers, but with what the pointers point to. You need to learn about dynamic memory allocation; I recommend picking up a good book and a cup of coffee. Commented Feb 29, 2012 at 22:34
  • Your add function looks ok, so I guess the problem is that you are calling add with the same second argument. If that is not the case please provide more information: how are you calling your add function ? Commented Feb 29, 2012 at 22:38
  • Geez, what is it with the kids these days and their need to typedef even simple, non-opaque data types? Commented Mar 1, 2012 at 4:53

3 Answers 3

4

You need to create a duplicate of the string.

i.e. in the add function you require the line

node->command = strdup(c);

In addition you will have to free this string to prevent memory leaks when you free the node.

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

Comments

1
node->command = c;

Should be something like:

// Allocate enough memory for a copy of the string pointed to by c
node->command = malloc(strlen(c) + 1);

// Copy the string pointed to by c, to the newly allocated memory
strcpy(node->command, c);

Or since you have to call strlen on c you can use the more efficient:

size_t num_bytes = strlen(c) + 1;

// Allocate enough memory for a copy of the string pointed to by c
node->command = malloc(num_bytes);

// Copy num_bytes bytes from the memory pointed to by c, to the newly allocated memory
memcpy(node->command, c, num_bytes);

So that you make a copy of the string. Your delete function should be modified to free the allocated memory as well:

free(node->command);

So what was wrong with your code?

Without copying the string you just copy the pointer, so if you call add(10, somestr); Then somestr is a char * (a pointer). If you modify the string at that memory location it will also be modified in your linked list since there is really only one string with two pointers to it (command and somestr).

Comments

0

I never knew one could assign a string to another using a '=' operator :P This may work fine with structure variables but not with strings. Try using strcpy(node->command, c) after dynamically allocating size to node->command. This should work! :)

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.