简体   繁体   English

重复发生 C2275 错误

[英]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:我在 Windowns 7 上使用 Microsoft Visual Studio 2010。由于某些无法理解的原因,当我尝试编译以下代码时,我不断收到 C2275 错误:

#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.编译时,我不断收到此错误“C2275:'node':非法使用此类型作为表达式”。 However, i asked one of my friends to paste the same code in his IDE it compiles on his system!!但是,我让我的一位朋友将相同的代码粘贴到他在他的系统上编译的 IDE 中!!
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.您不能在其他代码语句之后声明变量node * start 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.我不知道你为什么有**node

You simply need node *start ;你只需要node *start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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