0

First of all sorry if my question is a little bit silly but it is really important to learn from these stupid mistakes specially when I'm learning something new like Linked List in C programming language which is why I'm here, I'm implementing a simple linked list using a seperate function that insert a node(element) in the start of list but This problem always happens, I'll show you the code and tell me if I'm doing something wrong and thanx alot:

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

typedef struct element{
    int nb;
    struct element *next;
}e;
e Insert(e hd,int x){
    e *temp = (e*)malloc(sizeof(e));
    temp->nb=x;
    temp->next = hd; /*It shows that the error is here, all what im doing here is that im letting the temp element points to whichever the head elements in pointing at so it can take it place as the first element)*/
    return temp; /*here I'm returning the @ of temp to the head node*/
}
int main(int argc, char *argv[]) {
    e *head=NULL;
    head=Insert(head,5);
    system("PAUSE");    
    return 0;
}

and what the error says is : incompatible types in assignment

2
  • Don't you get a warning, calling Insert(e, int) as Insert(e*,int)? (head is an e *)? Commented May 15, 2015 at 17:56
  • 1
    Also your Insert() method returns an e *, not an e as you declared. Commented May 15, 2015 at 17:57

2 Answers 2

3

Insert() should pass e* in and returns e*.

e* Insert(e* hd,int x){
    e *temp = malloc(sizeof(e));
    temp->nb=x;
    temp->next = hd;
    return temp;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Insert should be taking e* hd as argument and returning e*. The method should be:

e* Insert(e* hd,int x){...}

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.