简体   繁体   English

通过函数参数从另一个文件访问静态全局数组

[英]Accessing static global array from another file via function argument

Getting segmentation fault while accessing the data from a static global array from another file; 从另一个文件访问静态全局数组中的数据时遇到分段错误; the pointer was passed as a function argument. 指针作为函数参数传递。 The memory address shows the same from both file. 内存地址在两个文件中显示相同。

In file1.c 在file1.c中

static long long T[12][16];
...
/* inside some function*/
  printf(" %p \n", T); // Check the address
  func_access_static(T);
...

In file2.c 在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
}

Am I trying to do something that can't be done? 我是否在尝试做一些无法完成的事情? Any suggestion is appreciated. 任何建议表示赞赏。

** is not the same thing than an array. **与数组不一样。

Declare your function 声明你的功能

void func_access_static(long long t[][16])

or 要么

void func_access_static(long long (*t)[16])

This is what a 2 dimensional array int t[2][3] looks like in memory 这就是二维数组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

This is what a pointer on pointer int ** looks like in memory 这就是指针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] |
                                             +---------+---------+---------+

To access the content it's the same syntax but the operation is quite different. 要访问内容,语法是相同的,但操作却大不相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM