我想在 main.c 中使用来自 foo.c 的变量 a,我写:
foo.c
#include <stdio.h>
int a[] = {3, 2};
void foo()
{
printf("foo\taddress of a:%x\n", a);
printf("foo\tvalue of a[0]:%x\n", a[0]);
}
main.c
#include <stdio.h>
extern int *a;
int main(void)
{
foo();
printf("main\taddress of a : %x\n", a);
printf("main\tvalue of a[0] : %x\n", a[0]);
return 0;
}
和结果输出:
foo address of a:804a014
foo value of a[0]:3
main address of a : 3
Segmentation fault (core dumped)
为什么?