当我涉及一个全局变量时,我在我的c代码中使用“extern”时有点困惑。 我尝试了以下操作,并收到了编译错误:
MAIN.C:
extern unsigned short *videobuffer;
//I also tried this in a separate and it failed with the same compilation error//
extern (unsigned short *)videobuffer;
lib.c:
unsigned short *videobuffer = (unsigned short *)0x6000000;
我收到的错误:
[COMPILE] Compiling main.c
main.c:16: error: expected identifier or '(' before 'unsigned'
make: *** [main.o] Error 1
答案 0 :(得分:0)
有效代码通常是人们想要的:
#include <stdio.h>
extern unsigned short *videobuffer;
int main() {
printf("%p\n", videobuffer);
}
unsigned short *videobuffer = (unsigned short *)0x6000000;
答案 1 :(得分:0)
以下是正确的:
MAIN.C:
extern unsigned short *videobuffer;
lib.c:
unsigned short *videobuffer = (unsigned short *)0x6000000;
extern
关键字告诉编译器有一个变量,但它不在这个编译单元中。