zorro
zorro

Reputation: 94

Array of pointers to 2D arrays, reads incorrect values

I need some help with accessing a 2D array with pointers.

I have 8 global char arrays declared like this:

char s1[4][16], s2[4][16], ... , s8[4][16];

Their values are set later in main function.

I have an array of 8 pointers to these arrays,

char (*s[8])[4][16];

Each pointer in this array is assigned like this:

s[0] = &s1;
s[1] = &s2;
..
..
s[7] = &s3;

Now to access elements of s1, I do *s[0][i][j], however I do not get the same values as those of s1. Same is the case for s2, s3 etc.

Can someone please tell what is going wrong?

Upvotes: 2

Views: 44

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

Index operator [] have higher precedence than indirect operator *, so you have to use parenthesis to dereference the pointer to the array.

Try doing (*s[0])[i][j].

Upvotes: 7

Related Questions