2D Arrays and Pointers - C

后端 未结 5 1852
春和景丽
春和景丽 2020-12-21 07:54

Just trying to really get my head round Arrays and Pointers in C and the differences between them and am having some trouble with 2d arrays.

For the normal 1D array

5条回答
  •  情书的邮戳
    2020-12-21 08:12

    If you really want to get through the bottom of it then try to understand arrays and pointers through ints rather than chars. According to my experience I had trouble understanding pointers and arrays when chars were involved. Once you understand ints properly you will realize that it's not diff at all.

    int *ptr[] is an array of pointers to integers where as int **ptr is a pointer to a pointer that references an integer.

    int *arrptrs[2];
    arrptrs[0]=(int *)malloc(sizeof(int)*5);
    arrptrs[1]=(int *)malloc(sizeof(int)5);
    This initializes two arrays referenced by the elements of the array arrptrs. The name of an array refers to the memory location of the first element of an array so arrptrs is of type (int *
    ) as the first element of this array is of type (int *)

    Suppose we do int **ptr=arrptrs Then, *ptr is the first element of arrptrs which is arrptrs[0]and *(ptr+1) is arrptrs[1] and doing a *arrptrs[0] is the first element in the array referenced by arrptrs[0].

    I hope this helps although I am not sure if you needed this.

提交回复
热议问题