+ Post New Thread
Results 1 to 2 of 2
-
6th November 2019, 15:30 #1
- Join Date
- Feb 2018
- Posts
- 19
- Helped
- 0 / 0
- Points
- 414
- Level
- 4
Creating Linked List
Hello
I want to make single linked list
When I compile code GCC compiler gives many warning
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <stdio.h> #include <stdlib.h> struct Node{ int N; struct Node *next; }; struct Node *addNode(struct node *next , int n ) { struct Node *new = malloc(sizeof(*new)); new->N = n; new->next = next; return new; } int main ( ) { struct Node *head = NULL; head = addNode(1, head); return 0; }
warning: 'struct node' declared inside parameter list will not be visible outside of this definition or declaration
struct Node *addNode(struct node *next , int n ) {
^~~~
: In function 'addNode':
warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
new->next = next;
^
In function 'main':
warning: passing argument 1 of 'addNode' makes pointer from integer without a cast [-Wint-conversion]
head = addNode(1, head);
^
note: expected 'struct node *' but argument is of type 'int'
struct Node *addNode(struct node *next , int n ) {
^~~~~~~
warning: passing argument 2 of 'addNode' makes integer from pointer without a cast [-Wint-conversion]
head = addNode(1, head);
^~~~
note: expected 'int' but argument is of type 'struct Node *'
struct Node *addNode(struct node *next , int n ) {
^~~~~~~
-
Advertisement
-
14th November 2019, 09:50 #2
- Join Date
- Feb 2002
- Posts
- 321
- Helped
- 30 / 30
- Points
- 4,745
- Level
- 16
Re: Creating Linked List
I think you need to understand c before attempting writing a linked list. You also need to understand the compiler warnings and errors. By looking at struct node, where is that declared? Also you returning a struct Node, what is the difference between the capitalize and lower case structs? Have you tried typedef?
I don't know your code but if you going to attempt to write a linked list there are plenty of examples online or in standard c book.
+ Post New Thread
Please login