簡體   English   中英

簡單程序無法在C中編譯

[英]Simple program won't compile in C

好的,我馬上知道這將是一個愚蠢的問題,但是我不明白為什么這個簡單的C程序無法編譯。

#include <stdio.h>
#include <stdlib.h>
typdef struct CELL *LIST;
struct CELL {
    int element;
    LIST next;
};
main() {
    struct CELL *value;
    printf("Hello, World!");
}

我是C語言編程的新手,而不是C語言編程的新手。我對Objective-C,Java,Matlab和其他一些語言很熟悉,但是由於某種原因,我無法弄清楚這一點。 我想在OS X中使用GCC編譯它,如果有所作為。 謝謝你的幫助!

我收到的錯誤消息是

functionTest.c:5: error: expected specifier-qualifier-list before ‘LIST’
functionTest.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’

主要原因是您將typedef typdeftypdef 但是,您還應該執行其他幾件事:

  • return 0; main()的末尾。
  • main的簽名更改為int main(void)

最重要的是:您拼寫了typedef。

然后,至少在最近幾天,我們通常在main中添加一個返回類型,如下所示:

int main()

另外,main應該返回退出狀態,因此:

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

typedef struct CELL *LIST;

struct CELL {
  int element;
  LIST next;
};

int main() {
  struct CELL *value;
  printf("Hello, World!\n");
  return 0;
}

您是否嘗試使用gcc -Wall -g yourprog.c -o yourbinary進行編譯?

我越來越:

yourprog.c:3:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
yourprog.c:6:5: error: unknown type name 'LIST'
yourprog.c:8:1: warning: return type defaults to 'int' [-Wreturn-type]
yourprog.c: In function 'main':
yourprog.c:9:18: warning: unused variable 'value' [-Wunused-variable]
yourprog.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]

並且您拼寫了typedef ,應該更改main的簽名並添加return 0; 內。

順便說一句,我發現您的typedef味道很差。 我建議編寫類似typedef struct CELL CELL_t代碼(就像Gtk一樣),並聲明CELL_t* value = NULL. 因為您確實想記住該value是指向CELL_t的指針。 特別是,我討厭typedef struct CELL* CELL_ptr;typedef struct CELL* CELL_ptr; 因為我發現非常重要(出於可讀性原因),以便快速了解什么是指針,什么不是指針。

其實我寧願建議

 struct cell_st;
 typedef struct cell_st cell_t;
 cell_t *value = NULL;

(我確實喜歡初始化所有指向NULL的指針)。

在主函數中添加收益

暫無
暫無

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

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