简体   繁体   中英

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.

I have an array of ints, it happens to be a 2d array.

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 ** . If you want to store a pointer to it inside the struct , you can do something like:

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:

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.

A multidimensional array is, to C, a one-dimensional array indexed in a different way. Say x is int [3][4] . Then, x contains 12 sequentially-packed elements, and x[1][2] is just the 6th element of that one-dimensional array.

A double-indirect pointer treated as a 2-dimensional array is an array of pointers to arrays . So, if y is int ** , then y[1][2] means "the third element of the array pointed to by the second element of 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).

Instead, you can cast int [M][N] as int (*)[N] -- a pointer to an array of N -length arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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