Hi

I am totally new to C programming (been using java for about 1 year) and have a question about pointers/linkedLists. So what i am trying to do is create a linked list and insert a new node as the head of the list...below is my code:

The error I am getting is
"list.c: In function ‘insertHead’:
list.c:25: error: incompatible types when assigning to type ‘int’ from type ‘struct node’"

I'm really unsure how I would assign the value of a node received as a parameter in the insertHead() function to a new node that I am creating. Thank you very much for any help :).

struct node* createNode(int d){
  struct node* newNode;

  newNode = malloc(sizeof(struct node));
  
  newNode->data = d;
  newNode->next = NULL;

  return newNode;
}


void insertHead(struct node **headRef, struct node *n){
  struct node* newNode = malloc(sizeof(struct node));

  newNode->data = *n;
  newNode->next = *headRef;
  *headRef = newNode; 
}

Can you show us how your calling insertHead()?...Ooops, its line 16 of the posted code. Your assigning struct node *n to newNode->data. Is newNode->data an int?

Can you show us how your calling insertHead()?...Ooops, its line 16 of the posted code. Your assigning struct node *n to newNode->data. Is newNode->data an int?

Yes newNode->data is an int, so what I am wanting to do is make newNode equal the *n node that the function is receiving...I'm just unsure as to how to do this. The node n also has a data in it that is of int type. So I'm basically trying to make one node equal another node....any ideas?

the function is being called like this, inside a loop

insertHead(&headRef, createNode(h++));

Try changing line 16 to

newNode->data = n->data;

Thanks, I have just tried that and now I am having another issue...problem is I have a file that tests my linkedList and the method in this file is now throwing an error:

test_list.c:17: error: dereferencing pointer to incomplete type
test_list.c:18: error: dereferencing pointer to incomplete type
(I highlighted these lines in the code below)

This method is not something I have created, its how I am supposed to test my linkedList and I'm not sure why this error keeps coming up.

void test1()
{
  struct node *headRef = NULL;
  struct node *nptr = NULL;
  int h = 0;

  while(h<5)
    insertHead(&headRef, createNode(h++));

  h = 0;
  for(nptr = headRef; nptr != NULL; nptr = nptr->next, h++)
    assert(nptr->item == (4-h));
  assert(h==5);
  }

If you look at your code you'll see that nptr is NULL. Your trying to dereference NULL.

In the while loop its creating 5 nodes right? so there should be a head reference and then in the for loop its saying nptr = headRef so shouldn't nptr not be null at this point?

I'm a little confused as to how this test method works and why its not assigning the headRef to nptr...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.