Create a pointer to two-dimensional array

后端 未结 10 1304
温柔的废话
温柔的废话 2020-11-22 07:16

I need a pointer to a static 2-dimensional array. How is this done?

static uint8_t l_matrix[10][20];

void test(){
   uint8_t **matrix_ptr = l_matrix; //wron         


        
10条回答
  •  终归单人心
    2020-11-22 07:36

    To fully understand this, you must grasp the following concepts:

    Arrays are not pointers!

    First of all (And it's been preached enough), arrays are not pointers. Instead, in most uses, they 'decay' to the address to their first element, which can be assigned to a pointer:

    int a[] = {1, 2, 3};
    
    int *p = a; // p now points to a[0]
    

    I assume it works this way so that the array's contents can be accessed without copying all of them. That's just a behavior of array types and is not meant to imply that they are same thing.



    Multidimensional arrays

    Multidimensional arrays are just a way to 'partition' memory in a way that the compiler/machine can understand and operate on.

    For instance, int a[4][3][5] = an array containing 4*3*5 (60) 'chunks' of integer-sized memory.

    The advantage over using int a[4][3][5] vs plain int b[60] is that they're now 'partitioned' (Easier to work with their 'chunks', if needed), and the program can now perform bound checking.

    In fact, int a[4][3][5] is stored exactly like int b[60] in memory - The only difference is that the program now manages it as if they're separate entities of certain sizes (Specifically, four groups of three groups of five).

    Keep in mind: Both int a[4][3][5] and int b[60] are the same in memory, and the only difference is how they're handled by the application/compiler

    {
      {1, 2, 3, 4, 5}
      {6, 7, 8, 9, 10}
      {11, 12, 13, 14, 15}
    }
    {
      {16, 17, 18, 19, 20}
      {21, 22, 23, 24, 25}
      {26, 27, 28, 29, 30}
    }
    {
      {31, 32, 33, 34, 35}
      {36, 37, 38, 39, 40}
      {41, 42, 43, 44, 45}
    }
    {
      {46, 47, 48, 49, 50}
      {51, 52, 53, 54, 55}
      {56, 57, 58, 59, 60}
    }
    

    From this, you can clearly see that each "partition" is just an array that the program keeps track of.



    Syntax

    Now, arrays are syntactically different from pointers. Specifically, this means the compiler/machine will treat them differently. This may seem like a no brainer, but take a look at this:

    int a[3][3];
    
    printf("%p %p", a, a[0]);
    

    The above example prints the same memory address twice, like this:

    0x7eb5a3b4 0x7eb5a3b4
    

    However, only one can be assigned to a pointer so directly:

    int *p1 = a[0]; // RIGHT !
    
    int *p2 = a; // WRONG !
    

    Why can't a be assigned to a pointer but a[0] can?

    This, simply, is a consequence of multidimensional arrays, and I'll explain why:

    At the level of 'a', we still see that we have another 'dimension' to look forward to. At the level of 'a[0]', however, we're already in the top dimension, so as far as the program is concerned we're just looking at a normal array.

    You may be asking:

    Why does it matter if the array is multidimensional in regards to making a pointer for it?

    It's best to think this way:

    A 'decay' from a multidimensional array is not just an address, but an address with partition data (AKA it still understands that its underlying data is made of other arrays), which consists of boundaries set by the array beyond the first dimension.

    This 'partition' logic cannot exist within a pointer unless we specify it:

    int a[4][5][95][8];
    
    int (*p)[5][95][8];
    
    p = a; // p = *a[0] // p = a+0
    

    Otherwise, the meaning of the array's sorting properties are lost.

    Also note the use of parenthesis around *p: int (*p)[5][95][8] - That's to specify that we're making a pointer with these bounds, not an array of pointers with these bounds: int *p[5][95][8]



    Conclusion

    Let's review:

    • Arrays decay to addresses if they have no other purpose in the used context
    • Multidimensional arrays are just arrays of arrays - Hence, the 'decayed' address will carry the burden of "I have sub dimensions"
    • Dimension data cannot exist in a pointer unless you give it to it.

    In brief: multidimensional arrays decay to addresses that carry the ability to understand their contents.

提交回复
热议问题