C中的外部语法?

时间:2015-10-24 22:03:57

标签: c extern gameboy

当我涉及一个全局变量时,我在我的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

2 个答案:

答案 0 :(得分:0)

有效代码通常是人们想要的:

的main.c

#include <stdio.h>
extern unsigned short *videobuffer;

int main() {
    printf("%p\n", videobuffer);
}

lib.c

unsigned short *videobuffer = (unsigned short *)0x6000000;

答案 1 :(得分:0)

以下是正确的:

MAIN.C:

extern unsigned short *videobuffer;

lib.c:

unsigned short *videobuffer = (unsigned short *)0x6000000;

extern关键字告诉编译器有一个变量,但它不在这个编译单元中。