从另一个文件访问静态全局数组中的数据时出现分段错误;指针作为函数参数传递。内存地址在两个文件中都显示相同的内容。
在file1.c中
static long long T[12][16];
...
/* inside some function*/
printf(" %p \n", T); // Check the address
func_access_static(T);
...
在file2.c
中void func_access_static(long long** t)
{
printf(" %p \n", t); // shows the same address
printf(" %lld \n", t[0][0]); // gives segmentation fault
}
我是否尝试做一些无法完成的事情?任何建议都表示赞赏。
答案 0 :(得分:2)
**
与数组不同。
声明你的功能
void func_access_static(long long t[][16])
或
void func_access_static(long long (*t)[16])
这就是二维数组int t[2][3]
在内存中的样子
t
t[0] t[1]
+---------+---------+---------+---------+---------+---------+
| t[0][0] | t[0][1] | t[0][2] | t[1][0] | t[1][1] | t[1][2] |
+---------+---------+---------+---------+---------+---------+
Contiguous memory cells of int type
这是指针int **
上的指针在内存中的样子
pointer pointer pointer
+---------+ +---------+---------+
| p | --> | p[0] | p[1] |
+---------+ +---------+---------+
| | Somewhere in memory
| | +---------+---------+---------+
| \---------->| p[1][0] | p[1][1] | p[1][2] |
| +---------+---------+---------+
|
| Somewhere else in memory
| +---------+---------+---------+
\-------------------->| p[0][0] | p[0][1] | p[0][2] |
+---------+---------+---------+
要访问内容,它的语法相同但操作完全不同。