开发平台(Platform): Win7
编译器: GCC
问题(Question):
程式利用scanf()读档案至int s[num], int t[num],
问题发生在写入 t[4] 时, s[0]的值跟着被覆蓋,
利用printf印出 t[4] 跟 s[0]的 address 发现两者相同...
想请问大神们, 明明是两个不同的ARRAY,
为何compiler 将两者的element 放在相同位址?
是否是小弟有疏忽,才造成这样的情形呢?
喂入的资料(Input):
5
1 2 4 6 8
3 5 7 9 10
预期的正确结果(Expected Output):
num = 5
t[i] = 3; s[0] = 1
t[i] = 5; s[0] = 1
t[i] = 7; s[0] = 1
t[i] = 9; s[0] = 1
t[i] = 10; s[0] = 1
错误结果(Wrong Output):
num = 5
t[i] = 3; s[0] = 1
t[i] = 5; s[0] = 1
t[i] = 7; s[0] = 1
t[i] = 9; s[0] = 1
t[i] = 10; s[0] = 10
&s[0] = 0xffffcb80, &t[4] = 0xffffcb80
程式码(Code):
#include <stdio.h>
int main(int argc, char const *argv[])
{
const int num;
int s[num];
int t[num];
scanf("%d", &num);
printf("num = %d\n", num);
for (int i = 0; i < num; ++i)
scanf("%d", &s[i]);
for (int i = 0; i < num; ++i) {
scanf("%d", &t[i]);
printf("t[i] = %d; s[0] = %d\n", t[i], s[0]);
}
printf("&s[0] = %p, &t[4] = %p\n", &s[0], &t[4]);
return 0;
}