Pointers , Multidimensional arrays and Addresses

前端 未结 6 718
悲&欢浪女
悲&欢浪女 2021-01-05 19:42

Lets say we have : int A [5] [2] [3]; Now, if I do : A[1][0][0] = 4; does that mean :

1.) A [1] and A [1][0] are pointers ?

2.) If A[1] is a pointer, then i

6条回答
  •  孤独总比滥情好
    2021-01-05 20:30

    Your assumptions would be true if you had dynamic arrays (i.e. allocated with malloc/calloc).

    Static arrays, however, are allocated as contiguous chunk of memory and are simply pointer to the first element. When you write A[X][Y][Z] it is basically equivalent to *(A + X*YSIZE*ZSIZE + Y*SIZE + Z), not to *(*(*(A+X) + Y) + Z). This allows for somewhat faster access to the data (you do not need to access intermediate pointers), but requires all the data to be allocated in one chunk and be of regular sizes.

    Here is more info regarding non-interchangeability of static vs. dynamic arrays in C.

提交回复
热议问题