Pointer to an array and Array of pointers

前端 未结 6 2057
感动是毒
感动是毒 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:36

    I'm not sure if i get the question right but I will try to point this out.

    There are pointers pointing to a type
    e.g.:

    int num;
    int* p_num = # // this is pointing at the int
    

    Moreover there are arrays (which in fact are pointers)

    int num;    // an Integer
    int* p_num; // a pointer. (can point at int's)
    int arr[3]; // an Array holding 3 int's
    arr[0] = 1; // + holding the values 1, 2, 3
    arr[1] = 2;
    arr[2] = 3;
    
    p_num = arr; // because an array is just a pointer "p_num" num is now pointing at
                 // + the first element in the array.
                 // + ** THIS IS NOW A POINTER TO AN ARRAY **
    num = *p_num;// num = 1
    

    And there are arrays, which can hold multiple pointers:

    int num1;
    int num2;
    int num3;
    
    int* p_array[3];    // an array holding 3 pointer to int's
    p_array[0] = &num1; // this is pointing at num1
    p_array[1] = &num2; // num2, ...
    p_array[2] = &num3;
                        // ** THAT IS AN ARRAY OF POINTERS **
    

提交回复
热议问题