Using pointers to get value from multidimensional array - C -
i trying value "second row" in multidimensional array. have problems that. thought numbers stored sequentialy in memory tab[2][2]
stored same tab[4]
. seems wrong.
this tried:
int b[2][2] = {{111,222},{333,444}}; int = 0; for(;i < 100; i++) printf("%d \n", **(b+i));
the problem 111
, 333
result. there no 222
or 444
in other 98 results. why?
the problem **(b+i)
doesn't think does. evaluates to:
b[i][0]
as matt mcnabb noted,
**(b+i)
is equivalent to:
*(*(b+i)+0)
and since *(b+i)
equivalent b[i]
, expression whole can seen as:
*(b[i]+0)
and hence:
b[i][0]
since array has 2 rows, values of i
0 , 1 in bounds of array (that's 111 , 333). rest wildly out of bounds.
what is:
#include <stdio.h> int main(void) { int b[2][2] = { { 111, 222 }, { 333, 444 } }; int *base = &b[0][0]; (int = 0; < 4; i++) printf("%d: %d\n", i, base[i]); return 0; }
output:
0: 111 1: 222 2: 333 3: 444
Comments
Post a Comment