Differences when using ** in C

前端 未结 11 977
半阙折子戏
半阙折子戏 2020-12-24 10:50

I started learning C recently, and I\'m having a problem understanding pointer syntax, for example when I write the following line:

int ** arr = NULL;
         


        
11条回答
  •  忘掉有多难
    2020-12-24 11:39

    The type of

    int ** arr;
    

    only have one valid interpretation. It is:

    arr is a pointer to a pointer to an integer
    

    If you have no more information than the declaration above, that is all you can know about it, i.e. if arr is probably initialized, it points to another pointer, which - if probably initialized - points to an integer.

    Assuming proper initialization, the only guaranteed valid way to use it is:

    **arr = 42;
    int a = **arr;
    

    However, C allows you to use it in multiple ways.

    • arr can be used as a pointer to a pointer to an integer (i.e. the basic case)

    int a = **arr;
    

    • arr can be used as a pointer to a pointer to an an array of integer

    int a = (*arr)[4];
    

    • arr can be used as a pointer to an array of pointers to integers

    int a = *(arr[4]);
    

    • arr can be used as a pointer to an array of pointers to arrays of integers

    int a = arr[4][4];
    

    In the last three cases it may look as if you have an array. However, the type is not an array. The type is always just a pointer to a pointer to an integer - the dereferencing is pointer arithmetic. It is nothing like a 2D array.

    To know which is valid for the program at hand, you need to look at the code initializing arr.

    Update

    For the updated part of the question:

    If you have:

    void foo(char** x) { .... };
    

    the only thing that you know for sure is that **x will give a char and *x will give you a char pointer (in both cases proper initialization of x is assumed).

    If you want to use x in another way, e.g. x[2] to get the third char pointer, it requires that the caller has initialized x so that it points to a memory area that has at least 3 consecutive char pointers. This can be described as a contract for calling foo.

提交回复
热议问题