接触C语言,不太清楚编译和链接过程,在测试函数中变量存放地址会不会发生变化的时候,谢了下面的代码:
void test(int k);
int main() {
int i = 0;
printf("The address of i is %x \n", &i);
test(i);
i++;
printf("The address of i is %x \n", &i);
test(i);
return 0;
}
//定义
void test(int k) {
static int o = k;
o++;
printf("The address of k is %x \n", &o);
}
然后就报错了:[Error] initializer element is not constant
但是我将代码中,静态变量的声明和定义分开时,却编译通过了?!
//声明
void test(int k);
int main() {
int i = 0;
printf("The address of i is %x \n", &i);
test(i);
i++;
printf("The address of i is %x \n", &i);
test(i);
return 0;
}
//定义
void test(int k) {
static int o;
o = k;
o++;
printf("The address of k is %x \n", &o);
}
这是为啥,搞不懂啊。
大家可不可以推荐一本书,真的很想琢磨清楚