Nob Wong
Nob Wong

Reputation: 239

assignment from incompatible pointer type in linked list?

I am new bee to C. I am currently writing linked list in C. When compiling, it keeps complaining about "assignment from incompatible pointer type". My code is like this:

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

struct snode{
  struct snode *next;
  int val;
};

struct slist{
  struct snode *head;
  struct snode *tail;
  int len;
};

struct slist *mylist;

void slist_insert(int v){
  struct snode *newnode;
  newnode = (struct snode*)malloc(sizeof(struct snode));
  newnode -> val = v;
  newnode -> next = NULL;


  if(mylist->head  = NULL){
    mylist->head  = malloc (sizeof(struct snode));
    mylist->tail = (struct snode*)malloc (sizeof(struct snode));
    mylist->head = newnode;
    mylist->tail = newnode;
    
  };

  else{
    mylist -> tail -> next = (struct snode*)malloc (sizeof(struct snode));
    mylist -> tail -> next = newnode;
  };
   mylist -> len +=1;  
};


main(){
    slist_insert(1);
    slist_insert(2);
    slist_insert(3);
    slist_insert(4);
    slist_insert(5);

    struct snode *temp;
    temp = (struct snode*)malloc(sizeof(struct snode));
    temp = mylist-> head;
    while(temp -> next != NULL){
      printf("%d\n", temp -> val);
      temp  = temp -> next;
    };
  };
    

Here is the modified one. I am using linux terminal to run this program. The compiler I am using is gcc -std=gnu99

UPDATE

slist.c: In function â:
slist.c:32: error: â without a previous â
slist.c: At top level:
slist.c:40: warning: return type defaults to â

Upvotes: 0

Views: 1265

Answers (2)

zwol
zwol

Reputation: 140836

There are many problems with this code. I am ONLY listing the problems that cause the "assignment from incompatible pointer" warnings. Compile with gcc -W -Wall and fix all the warnings. And after you've done that there will still be bugs.

  1. You need #include <stdlib.h>, right after the existing #include <stdio.h>, to make the declaration of malloc visible. (If you don't do this, the compiler is obliged to assume that malloc returns int, even though it knows perfectly well that that's wrong. This is what the "incompatible implicit declaration of built-in function ‘malloc’" warning is trying to tell you.)

  2. You are incorrectly casting struct snode allocations to struct slist * and then assigning them to struct snode * fields, in several places. Do not cast the return value of malloc to anything; just assign it. (Note that you may see people deliberately casting the return value of malloc in code that needs to be compilable as C++ as well as C. Do not do this until you are experienced enough to understand when it is necessary. If your compiler insists on treating everything as C++ even when it's meant to be C, get a better compiler.)

  3. struct snode's next field should be declared as struct snode *, not int *.

Upvotes: 4

Hagen von Eitzen
Hagen von Eitzen

Reputation: 2177

Your nextis a pointer to int, while you want it to be a pointer to struct snode, I assume. Moreover, I assume that your mylist is supposet to be an slist instead of a pointer to an slist. And as has been pointed out in the comments, that membres of that mylist are not necessarily initialized (implementaiton dependent. With your mode of using a pointer for mylist, you need to malloc (and init) that guy first ...

Upvotes: 2

Related Questions