Pointer to an array and Array of pointers

前端 未结 6 2075
感动是毒
感动是毒 2020-12-22 00:47

As I am just a learner, I am confused about the above question. How is a pointer to an array different from array of pointers? Please explain it to me, as I will have to exp

6条回答
  •  余生分开走
    2020-12-22 01:28

    POINTER TO AN ARRAY

    Pointer to an array will point to the starting address of the array.

    int *p; // p is a pointer to int
    int ArrayOfIntegers[5]; // ArrayOfIntegers is an array of 5 integers, 
                            // that means it can store 5 integers.
    
    p = ArrayOfIntegers; // p points to the first element of ArrayOfIntegers
    

    ARRAY OF POINTERS

    Array of pointers will contain multiple pointers pointing to different variables.

    int* ArrayOfPointers[2]; // An array of pointers which can store 2 pointers to int
    int A = 1;
    int B = 2;
    int *PointerToA ;
    PointerToA  = &A ; // PointerToA is a pointer to A
    int *PointerToB ; 
    PointerToB  = &B ; // // PointerToA is a pointer to A
    ArrayOfPointers[0] = PointerToA ; // ArrayOfPointers first element points to A
    ArrayOfPointers[1] = PointerToB ; // ArrayOfPointers second element points to B
    

提交回复
热议问题