简体   繁体   中英

Repeated occurrence of C2275 Error

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.

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));

I have no idea why you have **node there.

You simply need node *start ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM