繁体   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