简体   繁体   English

如何从C中的结构成员的指针访问数组中的值

[英]How to access value in array from a pointer that is a member of a struct in C

I've searched stackoverflow and seen every combination of the words in my question, but not the question I have. 我搜索了stackoverflow,并看到了问题中所有单词的组合,但没有发现我遇到的问题。

I have an array of ints, it happens to be a 2d array. 我有一个整数数组,它恰好是一个2D数组。

const int themap[something][something] = { {0, ...

I have a struct that I want to have a pointer to this array in my program 我有一个要在程序中指向此数组的指针的结构

typedef struct {
int** mymap;
} THE_STRUCT

In my program I want to iterate over the values of the array through the struct's pointer, but my data seems to be corrupted if i try to access it through the . 在我的程序中,我想通过结构的指针遍历数组的值,但是如果我尝试通过进行访问,则我的数据似乎已损坏。 syntax 句法

int value;
THE_STRUCT mystruct;
mystruct = (int**) themap;

...
//access the map data from mystruct's pointer?
value = mystruct.mymap[x][y];
//doesn't seem to return correct values

Taking the struct out of the picture the same exact function works if I directly use the array (as a global variable) 如果我直接使用数组(作为全局变量),则将结构从图片中取出,同样的精确功能将起作用

int value;
...
//access the map directly
value = themap[x][y]
//everyone is happy!

I would like to use the struct as in reality it will carry other information as well as the fact that I will need to be able to assign the pointer to other arrays with different data. 我想使用该结构,因为它实际上会携带其他信息,而且我需要能够将指针分配给具有不同数据的其他数组。

Your two-dimensional array is not the same as an int ** . 您的二维数组与int ** If you want to store a pointer to it inside the struct , you can do something like: 如果要在struct内部存储指向它的指针,则可以执行以下操作:

const int themap[something1][something2] = { {0, ...

typedef struct {
    const int (*mymap)[something2];
} THE_STRUCT;

...

THE_STRUCT my_struct;
my_struct.mymap = themap;

...

int value = my_struct.mymap[x][y];

It is possible to use an int ** , but it requires some effort: 可以使用int ** ,但它需要一些努力:

const int themap[something1][something2] = { {0, ...
const int * themapPointerArray[something1] = {themap[0], themap[1], ..., themap[something1 - 1]};

typedef struct {
    const int **mymap;
} THE_STRUCT;

...

THE_STRUCT my_struct;
my_struct.mymap = themapPointerArray;

...

int value = my_struct.mymap[x][y];

A multidimensional array int [][] and a double-indirect pointer int ** are two completely different things. 多维数组int [][]和双间接指针int **是完全不同的两件事。

A multidimensional array is, to C, a one-dimensional array indexed in a different way. 对于C,多维数组是以不同方式索引的一维数组。 Say x is int [3][4] . 假设xint [3][4] Then, x contains 12 sequentially-packed elements, and x[1][2] is just the 6th element of that one-dimensional array. 然后, x包含12个顺序打包的元素, x[1][2]只是该一维数组的第6个元素。

A double-indirect pointer treated as a 2-dimensional array is an array of pointers to arrays . 被视为二维数组的双间接指针是指向 array的指针的数组 So, if y is int ** , then y[1][2] means "the third element of the array pointed to by the second element of y ". 因此,如果yint ** ,则y[1][2]表示“由y的第二个元素指向的数组的第三个元素”。

You cannot therefore convert between int [][] and int ** , since they just represent different things (your casting of int [][] to int ** causes the integers in the int [][] array to be treated as pointers, which will inevitably crash). 因此,您无法在int [][]int **之间进行转换,因为它们只是表示不同的事物(将int [][]int **会导致将int [][]数组中的整数视为指针) ,这将不可避免地崩溃)。

Instead, you can cast int [M][N] as int (*)[N] -- a pointer to an array of N -length arrays. 相反,您可以将int [M][N]int (*)[N] -指向N长度数组的数组的指针。

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

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