2
 //linked list implementation 
 #include<stdio.h>
 #include<stdlib.h>
 struct node
 {
   int data;
   struct node* link;
  };
 struct node* head;
 void insert(int);
 void print();
 int main()
 {
   head=NULL;
   int n,i,x;
   printf("\nEnter the number of elements :");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
     printf("\nEnter the element :");
     scanf("%d",&x);
     insert(x);
     print();
   }
   }

   void insert(int x)
   {
     struct node* temp=(node*)malloc(sizeof(struct node));
     temp->data=x;
     temp->link=head;
     head=temp;
   }
   void print()
   {
     struct node* temp=head;
     int i=0;
     printf("\nThe list is ");
     while(temp!=NULL)
     {
       printf("%d ",temp->data);
       temp=temp->link;
     }
     printf("\n");
     }

While compiling the code:

In function 'insert':
28:24: error: 'node' undeclared (first use in this function)
     struct node* temp=(node*)malloc(sizeof(struct node));
                        ^
28:24: note: each undeclared identifier is reported only once for each function it appears in
28:29: error: expected expression before ')' token
     struct node* temp=(node*)malloc(sizeof(struct node));
                             ^
timrau
  • 22,578
  • 4
  • 51
  • 64
Ajithkumar_sekar
  • 631
  • 9
  • 23

2 Answers2

7
  1. node* is not the same as struct node* in this context in C as it is for C++.

  2. Try to avoid superfluous casts in C. Actually, try to avoid superfluous casts in any language where it is not required. Do I cast the result of malloc?

Community
  • 1
  • 1
1

Change the following statement

struct node* temp=(node*)malloc(sizeof(struct node));

into,

struct node* temp=(struct node*)malloc(sizeof(struct node));

Sometimes I do make similar mistakes.

John Park
  • 1,644
  • 1
  • 12
  • 17