An array in
C++is a collection of elements of the same data type, stored in contiguous memory locations. The elements can be accessed by theirindex, which is a numerical value that corresponds to the position of the element in thearray.
arr is a variable of type int* (pointer to int), which holds the memory address of the first element of an array. The variable arr can be used to access the elements of the array, for example, you can use arr[i] or i[arr] to access the i-th element of the array or *(arr+i) to access the i-th element of the array. Also you can use pointer arithmetic to move through the array, for example arr + 2 will point to the third element of the array
&arr is a variable of type int (*)[5], which holds the memory address of the entire array. The variable &arr can be used to get the size of the array. However, you cannot access the elements of the array using &arr directly, instead you need to use &arr[i] to access the i-th element of the array. Also you can't use pointer arithmetic to move through the array, for example &arr + 2 will not point to the third element of the array.
It is important to note that when arr is used in an expression, it is implicitly converted to a pointer to the first element of the array, which is why it has the same memory address as &arr[0]. Similarly, when &arr is used in an expression, it is implicitly converted to a pointer to the array, which is why it has the same memory address as arr. However, when you use *(arr) it will give you the value of the first element of the array, and when you use *(&arr) it will give you the entire array.
The variable arr is an array of integers with a size of 5. It is an lvalue, meaning it can be used on the left side of an assignment. &arr is a pointer to the array object, an rvalue with the value of the memory address of the array. They point to the same location in memory. To differentiate, compare *(arr) and *(&arr). The first is an lvalue of type int and the second an lvalue of type int [5].
In summary, arr and &arr are different variables with different types and behaviors, arr is a pointer to the first element of the array and &arr is a pointer to the entire array, they may have the same memory address but you can't use them in the same way.
Run it in your compiler for further clarification
*std::cout << (arr+1) << std::endl;
*std::cout << (&arr+1) << std::endl;
There are two ways to calculate the size of an array
- Dividing the total size of the array by the size of the first element sizeof(arr)/sizeof(arr[0]). This method is widely used.
- Subtracting the memory address of the last element of the array from the memory address of the first element: *(&arr+1)-arr.
When using the
pointer ptrto access elements of the array, adding 1 to it does not increment the memory address by 1 byte, but rather by the size of the data type of the array (in this case, an int). So when we say ptr + 1, it's equivalent to accessing the memory location that is 1 * sizeof(int) bytes away from the original memory location of ptr.
When an array is passed as a parameter to a function, a pointer to the first element of the array is created, which occupies actual memory. In the main function, the array(means array name ,i.e arr) itself does not occupy memory, but in a user-defined function, the pointer to the array does occupy memory. Therefore, the size of the array may not be correctly determined in a user-defined function by sizeof(arr)/sizeof(arr[0]) because arr is a pointer with actual memory ).
- It is not possible to determine the size of a
static arrayat runtime using the traditional method, however, using the concept of aconstant array size, the size of astatic arraycan be obtained during runtime. This allows for thedynamic allocation of memoryfor astaticarray, enabling its size to be determined and modified at runtime.
- When declaring an
array, it is possible to initialize specific elements of the array at that time by using the syntax of assigning a value to a specific index of the array. This allows for more flexibility in initializing arrays and can be useful in certain situation.
- It is also possible to initialize an array in a more concise way, by omitting the
=sign while providing the values to be assigned to thearray. This method is called uniform initialization . This way of initializing an array is considered to be more readable and can make the code more expressive. It is important to note that this method of initialization is only supported inC++11and later versions.
- In the context of a symbol table, the reason why we can't do arr++.The symbol table, which is used by the compiler to keep track of
variablesand theirtypes, does not contain information about the specific memory addresses of variables, but rather their types and scope.









