【发布时间】:2014-01-26 10:34:46
【问题描述】:
我知道我会在这个问题上得到很多降级,但我仍然编写以下测试代码:
int *gPtr;
//I know I can NOT write below code line, but I need know WHY
gPtr = NULL;//or some other value which I want to init it
//int *gPtr = NULL; //this line should be OK
int main (void)
{
int *ptr;
ptr = NULL;
return 0;
}
编译时全局*gPtr会输出错误:
ptr.c:4:1: warning: data definition has no type or storage class [enabled by default]
ptr.c:4:1: error: conflicting types for ‘gPtr’
ptr.c:3:6: note: previous declaration of ‘gPtr’ was here
ptr.c:4:8: warning: initialization makes integer from pointer without a cast [enabled by default]
但是,在函数中,我做了相同的代码,但没有编译错误/警告,我想知道:
- 在全局和函数中对值进行签名有什么区别。
- 为什么编译器不允许我在全局区域中对值进行签名。
-
int a = 0;和int a; a=0;//no other code between these two sentences有什么不同
请根据编译器的观点给我以上三个问题的建议(或者你认为在其他一些观点中应该有其他解释,比如编码标准?)
【问题讨论】:
-
int *gPtr = NULL;因为它是静态的,所以只需int *gPtr;就足够了。你不能;在这样的全局空间中有程序代码stmts。你可以在一个函数中。 Fwiw,我的编译器吐出一个完全不同的错误(“所有声明都需要类型说明符”)。
标签: c gcc compiler-construction