Pointers , Multidimensional arrays and Addresses

前端 未结 6 705
悲&欢浪女
悲&欢浪女 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:12

    Multidimentional arrays in c do not make use of pointers. Although a pointer-to-pointer-to-pointer access might look similar, The actual data may not be contiguous, and there is overhead to store all the addresses. Access of multidimentional arrays in C is a form of syntactic sugar:

    char a[5][7][9]
    a[d][h][w]  <<==>>  ((char*)a)[((9*7*d)+(9*h)+w] 
    

    C arrays all share the property that they decay(or automatically turn into pointers to the first element in the array). As a result,

    a[1]  (char[7][9])    --decay-->   ((*char)[5][9]) pointer to char array
    &a[1] ((*char)[5][9])  no decay
    

    The two are equivalent, because in the later you explicitly "decay" the pointer, whereas it happens automatically in the first.

提交回复
热议问题