Triple pointers in C: is it a matter of style?

后端 未结 5 1140
生来不讨喜
生来不讨喜 2020-12-04 20:18

I feel like triple pointers in C are looked at as \"bad\". For me, it makes sense to use them at times.

Starting from the basics, the single pointer

5条回答
  •  無奈伤痛
    2020-12-04 21:10

    Unfortunately you misunderstood the concept of pointer and arrays in C. Remember that arrays are not pointers.

    Starting from the basics, the single pointer has two purposes: to create an array, and to allow a function to change its contents (pass by reference):

    When you declare a pointer, then you need to initialize it before using it in the program. It can be done either by passing address of a variable to it or by dynamic memory allocation.
    In latter, pointer can be used as indexed arrays (but it is not an array).

    The double pointer can be a 2D array (or array of arrays, since each "column" or "row" need not be the same length). I personally like to use it when I need to pass a 1D array:

    Again wrong. Arrays are not pointers and vice-versa. A pointer to pointer is not the 2D array.
    I would suggest you to read the c-faq section 6. Arrays and Pointers.

提交回复
热议问题