-1

I just tried to implement linked list in C using the GCC compiler but I got these warnings. Most of the warnings I got were "assignment from incompatible pointer type":

linklist.c: In function 'insertatbegin':
linklist.c:20:8: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:24:18: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:25:8: warning: assignment from incompatible pointer type [enabled by default]
linklist.c: In function 'display':
linklist.c:37:7: warning: assignment from incompatible pointer type [enabled by default]

and after running the code I get nothing output.

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct 
{
int data;
struct node *next;
}node;
void insertatbegin(node **head,int item)
{
    node *nextnode=(node *)malloc(sizeof(node));
    if(nextnode!=NULL)
    {
        nextnode->data=item;
        nextnode->next=head;   //warning line 24
        head=nextnode;      // warning line 25
    }
    else
        printf("memory not allocated\n");

}

void display(node * head)
{
    node *temp=head;
    while(temp!=NULL)
    {
        printf(" %d  ",temp->data);
        temp=temp->next;   //warning line 37
    }
}

void main()
{
    node *head=NULL;

    insertatbegin(&head,20);
    insertatbegin(&head,30);
    insertatbegin(&head,40);
    display(head);
    getch();
}

It seems correct but I got nothing output from that.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • How many times is this going to be asked? Is the fact that C function arguments are pass-by-value so unintuitive? Is the syntax for declaring structs so hard to find using Google? –  Jun 16 '13 at 17:15
  • How many times are you going to exercise your search skills on SO to find a relevant duplicate? :D – Jonathan Leffler Jun 16 '13 at 17:16
  • @JonathanLeffler `(n + 1)` times, and this is that time! :D –  Jun 16 '13 at 17:17
  • Shekhar, look at the "Related" section on the right of this page for links to similar questions which might help you. – DOK Jun 16 '13 at 17:18
  • possible duplicate of [Passing pointer argument by reference under C?](http://stackoverflow.com/questions/1825794/passing-pointer-argument-by-reference-under-c) –  Jun 16 '13 at 17:26
  • @H2CO3: With due respect, the proposed duplicate is not a particularly good duplicate. It has nothing to do with the issue of `typedef struct { struct node *node; } node;` part of this question (though it does cover the `node *` vs `node **` part). I'd not be surprised to find there is a duplicate of that part of this question on SO, but I'm not sure I'm willing to spend the time finding it. – Jonathan Leffler Jun 16 '13 at 17:38
  • @JonathanLeffler I feel that quite often too. Yes, I'm aware that it's only half a dupe, but I think that's the *really annoying and repetitive* part. The question of the untagged vs. tagged struct is not that bad, actually. –  Jun 16 '13 at 17:39

1 Answers1

1

You show the typedef:

typedef struct 
{
int data;
struct node *next;
}node;

Here, you have an untagged structure type with the typedef name node, and an unrelated tagged structure type struct node (the details of which are not defined in the code you show).

You need:

typedef struct node
{
    int data;
    struct node *next;
} node;

Now node and struct node refer to the same type. Remember, typedef introduces an alias for another type; it does not introduce a distinct type.

Since the line you mark as 24 in the code here is line 16 in the code shown, it is difficult to know where line 20 really is. It is not obvious why you get a warning at line 20 as:

linklist.c:20:8: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:24:18: warning: assignment from incompatible pointer type [enabled by default]
linklist.c:25:8: warning: assignment from incompatible pointer type [enabled by default]

The warnings at lines 24 and 25 are because you have:

nextnode->next=head;   //warning line 24
head=nextnode;      // warning line 25

and you need:

nextnode->next = *head;   //warning line 24
*head = nextnode;      // warning line 25

This is because head is a node ** yet nextnode and nextnode->next are both node * (with the fixed structure definition). The warnings at line 37 is due to the struct node vs node confusion; you'd also get that warning for line 24 if you just fixed the assignment as shown above.

Also, as noted in the comments, the return type for main() should be int, not void (because that's what the C standard says is the return type of main()). If you are using a C89 compiler (which #include <conio.h> suggests you are), you should explicitly return a status from the main() function. C99 and later allows you to omit that return; IMNSHO, it is better to be explicit about it and return a value from every function that is declared to return a value.

Also note that you should output a newline somewhere; there is no guarantee that anything will be displayed until you do output a newline.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278