Pointer to an array and Array of pointers

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

    Background

    Think of pointers as just a separate data type. They have their own storage requirements -- such as their size -- they occupy 8 bytes on a x86_64 platform. This is the case of void pointers void*.

    In those 8 bytes the information stored is the memory address of another piece of data.

    The thing about pointers is that since they "point" to another piece of data, it's useful to know what type that data is too so you can correctly handle it (know its size, and structure).

    In stead of having their own data type name such as pointer they compose their name based on the data type they refer to such as int* a pointer to an integer. If you want a plain pointer without type information attached to it you have the option of using void*.

    So basically each pointer (to int, to char, to double) is just a void* (same size, same use) but the compiler knows the data being pointed to is of type int and allows you to handle it accordingly.

    /**
     *  Create a new pointer to an unknown type.
     */
    void* data;
    
    /**
     *  Allocate some memory for it using malloc
     *  and tell your pointer to point to this new
     *  memory address (because malloc returns void*).
     *  I've allocated 8 bytes (char is one byte).
     */
    data = malloc(sizeof(char)*8);
    
    /**
     *  Use the pointer as a double by casting it
     *  and passing it to functions.
     */
    double* p = (double* )data;
    p = 20.5;
    pow((double* )data, 2);
    

    Pointer to array

    If you have an array of values (let's say integers) somewhere in memory, a pointer to it is one variable containing its address.

    You can access this array of values by first dereferencing the pointer and then operating some work on the array and its values.

    /**
     *  Create an array containing integers.
     */
    int array[30];
    array[0] = 0;
    array[1] = 1;
    ...
    array[29] = 29;
    
    /**
     *  Create a pointer to an array.
     */
    int (*pointer)[30];
    
    /**
     *  Tell the pointer where the data is.
     */
    pointer = &array;
    
    /**
     *  Access the data through the pointer.
     */
    (*pointer)[1] = 999;
    
    /**
     *  Print the data through the array.
     *  ...and notice the output.
     */
    printf("%d", array[1]);
    

    Array of pointers

    If you have an array of pointers to values, the entire array of pointers is one variable and each pointer in the array refers to somewhere else in the memory where a value is located.

    You can access this array and the pointers inside it without dereferencing it but in order to reach a certain value from it you will have to dereference one of the pointers inside the array.

    /**
     *  Create an array containing pointers to integers.
     */
    int *array_of_pointers[30];
    array_of_pointers[0] = 0;
    array_of_pointers[1] = 1;
    ...
    array_of_pointers[29] = 29;
    

提交回复
热议问题