Home
Sign Up
Sign In
V2EX
›
C
为何这段程序编译时会出现"redefinition"错误?
013231
·
Jul 15, 2012
· 2672 views
This topic created in 5224 days ago, the information mentioned may be changed or developed.
按照我的理解, 就是声明了一个变量p, 类型void(*)(), 然后用&fun给它赋值, 为什么会出错呢?
void(*p)() = &fun;
这样在定义时直接赋值就不会出错.
void
Fun
赋值
4 replies
•
1970-01-01 08:00:00 +08:00
hehuozhao
Jul 15, 2012
1
我用VS2010跑了一下是正确的,如果你把void (*p)();和p = &fun;放在main()里面的话。
013231
OP
Jul 15, 2012
2
@
hehuozhao
问题正出在这里. C语言中, 声明可以在全局作用域进行, 但赋值不行. 所以"void(*p)() = &fun;"成功, "void (*p)();p = &fun;"失败.
013231
OP
Jul 15, 2012
3
我先前的解释有误. 具体见此:
http://stackoverflow.com/questions/11492731/why-does-this-code-cause-compilation-error-redefinition?answertab=votes#tab-top
xpfd
Jul 16, 2012
4
p = &fun; 赋值语句,只能放在函数体内。
About
·
Help
·
Advertise
·
Blog
·
API
·
FAQ
·
Privacy
·
Solana
·
5586 Online
Highest 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 34ms ·
UTC 03:14
·
PVG 11:14
·
LAX 19:14
·
JFK 22:14
♥ Do have faith in what you're doing.
❯