Pointers , Multidimensional arrays and Addresses

前端 未结 6 682
悲&欢浪女
悲&欢浪女 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.

    0 讨论(0)
  • 2021-01-05 20:12

    Read up on Row Major Order

    Yeah, that didn't help at all, I was thinking C was column major for a second there I think. (forgetting what all the stuff meant)

    0 讨论(0)
  • 2021-01-05 20:16

    It might be a little weird, but here's my explanation. Imagine you have six distinct people numbered from 1 to 6, and they are lined up accordingly from 1 to 6. If you told that this team (the six people) is divided into two groups, where the first three (1-3) are in group A, and the rest (4-6) are in group B.

    [1 2 3] [4 5 6]

    Then, if I tell you who is the first one in the TEAM? You would say the first one! But if I ask you who is the first member in GROUP A of THE TEAM? It is the same, the first person!

    Also, if I tell you who is the fourth member in the team? You would say the fourth! Then, what if I ask you who is the first member in GROUP B of THE TEAM? It is the same, the fourth one.


    The same story happens to you; A[1] is a pointer to beginning of the big array (The Team), where A[1][0] tells to point to the the beginning of the first inner array (THE FIRST GROUP) inside the big array (A[1]), which is identical! Then you say, &A[1][0][0], which is like going to ask the first member of the first inner array which resides in the big array, what is your number? Then, he will replay the same.

    What differs is the type of the pointers and how they are interpreted, but their values are identical. This because arrays stores elements in contiguous way.

    0 讨论(0)
  • 2021-01-05 20:25

    The variable A is 5 * 2 * 3 ints, allocated together as a block. (I.e., 30 ints).

    There are no pointers involved in the declaration `int A[5][2][3];' - the only space set aside is to hold the 30 int values.

    When you write an expression using A and subscripts, because you have said there are 3 dimensions, you must provide all 3 in order to specify which int value you are accessing or altering. If you use fewer than 3 subscripts, you are only partially specifying what you are accessing; the convention is that such a reference is taken as a request for the address of the relevant part of the overall space.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 20:31

    To properly answer your question, read up on row-major order, which is how multidimensional arrays are stored in C. The Wikipedia article is a little too terse, but one of these might be more clear:

    http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html http://archive.gamedev.net/archive/reference/articles/article1697.html http://www.ibiblio.org/pub/languages/fortran/append-c.html

    There's this SO question also.


    In direct answer to your points, assuming that you know how row-major storage works:

    int A[5][2][3] declares a contiguous region of memory that is 5*2*3 ints long: five arrays of two arrays of three ints each. The arrays are stored next to each other in linear memory, so that

    &A[0][0][0] == A
    &A[0][0][1] == A+1
    &A[0][1][0] == A+(1*3)
    &A[3][1][2] == A+(3*(2*3))+(1*3)+2
    

    A[1] is technically not a pointer but an array. It's an int [2][3] array. But I find that much less clear to think about than considering A[5][2][3] is a flat region of memory, thirty ints long.

    A[0][0][0] is the first integer in that region. 
    A[0][0][1] is the second integer. 
    A[0][0][2] is the third integer. 
    A[0][1][0] is the fourth integer in this flat region. 
    A[0][1][1] is the fifth integer. 
    And so on until A[1][0][0] is the eleventh integer. 
    

    Therefore the address of A[1][0][0] is ten integers past A[0][0][0]; ie, &A[1][0][0] - &A[0][0][0] == 10. Because the C language is very loose about the difference between arrays and pointers, A[1] gets interpreted as if it were an address when you use it in an expression, even though it really means "the first element in an array of five arrays of two arrays of three integers" which is in turn "an array of two arrays of three integers."

    The upshot is that A[1] doesn't store a pointer, it is a pointer. Every memory address from &A[0][0][0] to &A[5][2][3]-1 stores an integer in your multidimensional array.

    What you're thinking of in points (2) and (3) are arrays of pointers to arrays, which are something different.

    This is a lot easier to explain with pictures, which is why you should find a proper textbook or article on C arrays.

    In general, when learning about pointers and arrays in C, I recommend that you temporarily forget about the language itself and pretend that you are Dennis Ritchie inventing C on a PDP-11 computer with 56kb of flat RAM. Get a big sheet of graph paper, number its cells consecutively, pretend that it represents your RAM and each cell is one byte, and you can work through your pointer math with pencil and paper.

    C was invented in that environment and understanding its origins will make the modern langauge much more sensible.

    As a side note, when I tried to write this answer, Stack Overflow's markup language repeatedly changed and screwed up the indexes in my array examples above. So if you see any numbers there that seem out of range for their arrays, it's a mistake introduced by the editor.

    0 讨论(0)
提交回复
热议问题