C pointer to array/array of pointers disambiguation

前端 未结 13 1370
我寻月下人不归
我寻月下人不归 2020-11-21 05:19

What is the difference between the following declarations:

int* arr1[8];
int (*arr2)[8];
int *(arr3[8]);

What is the general rule for under

相关标签:
13条回答
  • 2020-11-21 06:12

    The answer for the last two can also be deducted from the golden rule in C:

    Declaration follows use.

    int (*arr2)[8];

    What happens if you dereference arr2? You get an array of 8 integers.

    int *(arr3[8]);

    What happens if you take an element from arr3? You get a pointer to an integer.

    This also helps when dealing with pointers to functions. To take sigjuice's example:

    float *(*x)(void )

    What happens when you dereference x? You get a function that you can call with no arguments. What happens when you call it? It will return a pointer to a float.

    Operator precedence is always tricky, though. However, using parentheses can actually also be confusing because declaration follows use. At least, to me, intuitively arr2 looks like an array of 8 pointers to ints, but it is actually the other way around. Just takes some getting used to. Reason enough to always add a comment to these declarations, if you ask me :)

    edit: example

    By the way, I just stumbled across the following situation: a function that has a static matrix and that uses pointer arithmetic to see if the row pointer is out of bounds. Example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUM_ELEM(ar) (sizeof(ar) / sizeof((ar)[0]))
    
    int *
    put_off(const int newrow[2])
    {
        static int mymatrix[3][2];
        static int (*rowp)[2] = mymatrix;
        int (* const border)[] = mymatrix + NUM_ELEM(mymatrix);
    
        memcpy(rowp, newrow, sizeof(*rowp));
        rowp += 1;
        if (rowp == border) {
            rowp = mymatrix;
        }
    
        return *rowp;
    }
    
    int
    main(int argc, char *argv[])
    {
        int i = 0;
        int row[2] = {0, 1};
        int *rout;
    
        for (i = 0; i &lt; 6; i++) {
            row[0] = i;
            row[1] += i;
            rout = put_off(row);
            printf("%d (%p): [%d, %d]\n", i, (void *) rout, rout[0], rout[1]);
        }
    
        return 0;
    }
    

    Output:

    0 (0x804a02c): [0, 0]
    1 (0x804a034): [0, 0]
    2 (0x804a024): [0, 1]
    3 (0x804a02c): [1, 2]
    4 (0x804a034): [2, 4]
    5 (0x804a024): [3, 7]
    

    Note that the value of border never changes, so the compiler can optimize that away. This is different from what you might initially want to use: const int (*border)[3]: that declares border as a pointer to an array of 3 integers that will not change value as long as the variable exists. However, that pointer may be pointed to any other such array at any time. We want that kind of behaviour for the argument, instead (because this function does not change any of those integers). Declaration follows use.

    (p.s.: feel free to improve this sample!)

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