Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated occurrence of C2275 Error

Tags:

c

I am using Microsoft Visual Studio 2010 on Windowns 7. For some un-understandable reason i keep getting the C2275 error when i try to compile the following code:

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

typedef struct list_node   
{  
   int x;   
   struct list_node *next;  
}node;  

node* uniq(int *a, unsigned alen)   
{  
   if (alen == 0)   
          return NULL;    
   node *start = (node*)malloc(sizeof(node));   //this is where i keep getting the error   
   if (start == NULL)   
          exit(EXIT_FAILURE);    
   start->x = a[0];    
   start->next = NULL;     
   for (int i = 1 ; i < alen ; ++i)   
   {
          node *n = start;  
          for (;; n = n->next)  
          {  
                 if (a[i] == n->x) break;  
                 if (n->next == NULL)   
                 {  
                       n->next = (node*)malloc(sizeof(node));  
                       n = n->next;  
                       if (n == NULL)   
                              exit(EXIT_FAILURE);  
                       n->x = a[i];   
                       n->next = NULL;  
                       break;  
                 }  
          }  
   }  
   return start;  
}  

int main(void)  
{
   int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};  
   /*code for printing unique entries from the above array*/  
   for (node *n = uniq(a, 10) ; n != NULL ; n = n->next)  
          printf("%d ", n->x);    puts("");    
   return 0;  
}  

I keep getting this error "C2275: 'node' : illegal use of this type as an expression" when i compile. However, i asked one of my friends to paste the same code in his IDE it compiles on his system!!
I would like to understand why the behaviour of the compiler is different on different systems and what influences this difference in behavior.

like image 661
Madhusudan Avatar asked Dec 06 '25 06:12

Madhusudan


1 Answers

You can't declare a variable node * start after other code statements. All declarations have to be at the start of the block.

So your code should read:

node * start;

if (alen == 0) 
    return;

start = malloc(sizeof(*start));
like image 130
Vicky Avatar answered Dec 08 '25 20:12

Vicky