简体   繁体   English

简单程序无法在C中编译

[英]Simple program won't compile in C

Okay I know right off the bat this is going to be a stupid question, but I am not seeing why this simple C program is not compiling. 好的,我马上知道这将是一个愚蠢的问题,但是我不明白为什么这个简单的C程序无法编译。

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

I am new to C programming, not to programming in general, but to C. I am familiar with Objective-C, Java, Matlab, and a few others, but for some reason I can not figure this one out. 我是C语言编程的新手,而不是C语言编程的新手。我对Objective-C,Java,Matlab和其他一些语言很熟悉,但是由于某种原因,我无法弄清楚这一点。 I am trying to compile it using GCC in OS X if that makes a difference. 我想在OS X中使用GCC编译它,如果有所作为。 Thanks for your help! 谢谢你的帮助!

The error message I am getting is 我收到的错误消息是

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

The main reason is that you typoed typedef as typdef . 主要原因是您将typedef typdeftypdef However, there are a couple other things you should do: 但是,您还应该执行其他几件事:

  • Add return 0; return 0; to the end of main() . main()的末尾。
  • Change the signature of main to int main(void) main的签名更改为int main(void)

Most importantly: You have misspelled typedef. 最重要的是:您拼写了typedef。

Then, at least these days, we normally add a return type to main, like so: 然后,至少在最近几天,我们通常在main中添加一个返回类型,如下所示:

int main()

Also, main is supposed to return the exit status, so: 另外,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;
}

Did you try to compile it with gcc -Wall -g yourprog.c -o yourbinary ? 您是否尝试使用gcc -Wall -g yourprog.c -o yourbinary进行编译?

I'm getting: 我越来越:

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]

and you mispelled typedef and you should change the signature of main and add a return 0; 并且您拼写了typedef ,应该更改main的签名并添加return 0; inside. 内。

By the way, I find your typedef very poor taste. 顺便说一句,我发现您的typedef味道很差。 I suggest to code (like Gtk does) something like typedef struct CELL CELL_t and declare CELL_t* value = NULL. 我建议编写类似typedef struct CELL CELL_t代码(就像Gtk一样),并声明CELL_t* value = NULL. because you really want to remember that value is a pointer to CELL_t . 因为您确实想记住该value是指向CELL_t的指针。 In particular, I hate typedef-s like typedef struct CELL* CELL_ptr; 特别是,我讨厌typedef struct CELL* CELL_ptr;typedef struct CELL* CELL_ptr; because I find very imporrtant (for readability reasons) to quickly understand what is a pointer and what is not a pointer. 因为我发现非常重要(出于可读性原因),以便快速了解什么是指针,什么不是指针。

Actually I would rather suggest 其实我宁愿建议

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

(I do like initializing all pointers to NULL). (我确实喜欢初始化所有指向NULL的指针)。

在主函数中添加收益

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

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