I have an array arr
say
int arr[3] = {1, 2, 3};
so as per my knowledge this arr
is just like any variable that has a name arr
just like
int a = 4;
a
is an integer variable that asks 4
bytes of memory. And arr
asks 3
* 4
= 12
bytes of memory and arr
has properties like 12
bytes of memory and when used as an Rvalue it decays to a
(pointer value not a pointer variable)
so as it decays to a pointer value like &arr[0]
we can use a pointer variable to point (grab the address)
int* ptr = arr; //here
arr
implicitly converts to&arr[0]
so here arr
and ptr
have the same address but ptr
is like copied the memory of &arr[0]
and stores it. Whereas arr
is literally the address in the memory which only decays like &arr[0]
when used as Rvalue.
if we do something like
arr++;
complier throws error as Expression must be a modifiable lvalue
As the arr
is allocated on the stack we cannot change the address
because we are asking cpu to change the address of the array, if we change it we loose the array address and cannot further access it
Code
#include <stdio.h>
int main()
{
int arr[3] = { 1, 2, 3 };
arr++; // not allowed
int* p = arr;
p++; // allowed because its a copy of (arr) address.
return 0;
}
That's how what i know and correct me with more resources.