簡體   English   中英

重復發生 C2275 錯誤

[英]Repeated occurrence of C2275 Error

我在 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;  
}  

編譯時,我不斷收到此錯誤“C2275:'node':非法使用此類型作為表達式”。 但是,我讓我的一位朋友將相同的代碼粘貼到他在他的系統上編譯的 IDE 中!!
我想了解為什么編譯器的行為在不同的系統上是不同的,以及是什么影響了這種行為的差異。

您不能在其他代碼語句之后聲明變量node * start 所有聲明都必須在塊的開頭。

所以你的代碼應該是:

node * start;

if (alen == 0) 
    return;

start = malloc(sizeof(*start));

我不知道你為什么有**node

你只需要node *start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM