Differences when using ** in C

前端 未结 11 921
半阙折子戏
半阙折子戏 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:46

    How can I know if :

    • arr is a pointer to a pointer of an integer

    It is always a pointer to pointer to integer.

    • arr is a pointer to an array of pointers to integers
    • arr is a pointer to an array of pointers to arrays of integers

    It can never be that. A pointer to an array of pointers to integers would be declared like this:

    int* (*arr)[n]
    

    It sounds as if you have been tricked to use int** by poor teachers/books/tutorials. It is almost always incorrect practice, as explained here and here and ( with detailed explanation about array pointers) here.

    EDIT

    Finally got around to writing a detailed post explaining what arrays are, what look-up tables are, why the latter are bad and what you should use instead: Correctly allocating multi-dimensional arrays.

提交回复
热议问题