1

I'm attempting to use a simple linked list example, in attempt to understand the basic idea behind using them, for later use. However, I've gotten confused about how to set each node in the list to a certain value. I.e here, I want to set member "a" to the address of "b" and member "b" to the address of c. However, this is where the warning occurs,

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

struct List
{
    int data;
    struct List * next;
};

int main (int argc, char * argv[])
{

    struct List * a = malloc(sizeof(struct List));
    a->data = 0;
    a->next = NULL;

    struct List * b = malloc(sizeof(struct List));
    b->data = 1;
    b->next = NULL;

    struct List * c = malloc(sizeof(struct List));
    c->data = 2;
    c->next = NULL;

    a->next = &b; //warning occurs here
    b->next = &c;
}

Is there a way to set the values of a->next (a.next) and b->next(b.next) without any warning whatsoever?

3
  • You want a->next to point to the space allocated by malloc to which b is pointing. Not to the pointer b. Commented Mar 17, 2015 at 23:41
  • So the first one and last one (malloc calls) are fine then? just the * b one? Commented Mar 17, 2015 at 23:44
  • All the malloc calls are fine, the problems are the two lines giving you the warning Commented Mar 17, 2015 at 23:48

2 Answers 2

1

a->next is of type struct List *.

b is of type struct List *.

&b is of type struct List **.

You probably meant to do this: a->next = b;

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

Comments

0

b is already a pointer to a struct List so you don't need the & operator.

Just do:

a->next = b;

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.