The int pointer is not an int array. But your bigger question, apparently, is the reason that both arrays and pointers are needed.
An array represents the actual storage in the data memory. Once this storage is allocated, it does not make a significant difference whether you are referencing data stored using array notation or pointer notation.
However, this storage can also be allocated without using array entries, which means that arrays are not necessary. The main advantage of arrays is the convenient distribution of small blocks of memory, i.e. int x[20] and a slightly more convenient notation is array[i] , not *(array+i) . Fortunately, this more convenient notation can be used regardless of whether the array came from an array declaration or just a pointer. (In fact, as soon as the array was allocated, its variable name from this point does not differ from the pointer, which was assigned to indicate the memory location of the first value in the array.)
Note that the compiler will complain if you try to directly allocate an oversized block of memory in the array.
Arrays:
- represent the actual memory that is allocated
- the name of the array variable is the same as the pointer that refers to the point in memory where the array begins (and the name of the variable + 1 matches the pointer that refers to the point in memory where the second element is an array (if it exists), etc. )
- values ββin an array can be accessed using an array entry of type
array[i]
Pointers
- is a place to store the location of something in memory.
- may refer to memory allocated in the array
- or it may refer to memory that was allocated to functions such as
malloc - the value stored in the memory pointed to by the pointer can be obtained by dereferencing the pointer, i.e.
*pointer . - since the name of the array is also a pointer, the first element of the array can be accessed through
*array , the second element - * (array + 1), etc. - an integer can be added or subtracted to the pointer to create a new pointer pointing to other values ββin the same block of memory that was allocated by your program. For example, array + 5 indicates the place in memory where the array of values ββis stored [5].
- the pointer may increase or decrease, indicating other values ββwith the same memory block.
In many situations, one notation will be more convenient than the other, so itβs very useful that both notations are available and are so easily interchangeable with each other.
source share