【发布时间】:2014-09-12 13:56:05
【问题描述】:
我正在关注这个example,我的程序如下所示:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct Foo
{
int x;
int array[100];
};
struct Foo f;
f.x = 54;
f.array[3]=9;
void print(void){
printf("%u", f.x);
}
int main(){
print();
}
但是,我在使用 make example_1 编译时遇到错误:
example_1.c:13:1: error: unknown type name 'f'
f.x = 54;
^
example_1.c:13:2: error: expected identifier or '('
f.x = 54;
^
example_1.c:14:1: error: unknown type name 'f'
f.array[3]=9;
^
example_1.c:14:2: error: expected identifier or '('
f.array[3]=9;
^
4 errors generated.
make: *** [example_1] Error 1
这个结构声明有什么问题?
【问题讨论】:
-
您不能在函数之外编写可执行代码。将 3 行
struct Foo f; ... f.array[3]=9放入main()。
标签: c arrays struct initialization