1

As most beginners in C, I'm implementing a simple linked list.

Each node for the list is defined as so:

typedef struct list_node {
    void * data;
    struct list_node * next;
} list_node_t;

I made a test program to create and iterate a list. Creation and insertion operations work correctly. However, the code below is giving me problems:

list_node_t * node = NULL;

list_iter_reset(list, node);

if (node == NULL) {
    printf("Node is NULL.\n");
} else {
    printf("Node is not NULL.\n");
}
fflush(stdout);

The function list_iter_reset() is defined below. Please note that list->head does not point to NULL, as I have inserted nodes in the list previously.

void list_iter_reset(list_t list, list_node_t * node)
{
    node = list->head;

    if (node == NULL) {
        printf("Node is NULL.\n");
    } else {
        printf("Node is not NULL.\n");
    }
    fflush(stdout);
}

The output from executing that code is as follows:

Node is not NULL.
Node is NULL.

Since I'm passing a pointer to the node to the function, why is the created node in the test program still pointing to NULL after that function call?

I'm guessing a simple pointer arithmetic aspect went over my head here. I've been looking around and could not find a similar issue.

Thank you in advance.

8
  • 2
    Seems you want to be passing a pointer to pointer (list_node_t**) to the function, not a pointer. The function is modifying the value of the pointer (node), but that never gets back to the caller. Commented Dec 9, 2018 at 16:29
  • 1
    I think you need to pass the node pointer as a reference list_node* &node Commented Dec 9, 2018 at 16:31
  • Thank you for the reply! This might sound ingenuous, but shouldn't the pointer to the node be all I need to change its value? Since I'm passing the memory address of the node to the function, why would I need the "second level" of memory access? Commented Dec 9, 2018 at 16:32
  • 2
    @GregK This is c. There are no references. Commented Dec 9, 2018 at 16:32
  • 2
    @SaucyGoat Great question! A pointer is all you need to change the value of the thing it's pointing to. But here, you are trying to change the value of the pointer itself. It takes a while to wrap your head around it but seems like you're on the right track. As a comparison, imagine you would be passing an int to the function an expecting the function to change that int. You would of course have to pass an int*, right? This is the same thing, except replace int with node*, and so you need to pass a node**. Commented Dec 9, 2018 at 16:34

1 Answer 1

1

The function is modifying the value of the pointer, but that never gets back to the caller. It seems you want to be passing a pointer to pointer (list_node_t**) to the function, not a regular pointer.

void list_iter_reset(list_t list, list_node_t** node)
{
    *node = list->head;

    ...
}

The reasoning for this is that while a pointer is all you need to change the value of the thing it's pointing to, here you are trying to change the value of the pointer itself, i.e. where this pointer is pointing to.

As a comparison, imagine you would be passing an int to the function and expecting the function to modify that int. You would of course have to pass an int*, right? This is the same thing, except replace int with node*, and so you need to pass a pointer to that type, which in this case is node**

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

1 Comment

The if should also be changed to use *node.

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.