int a[20];
You are declaring an array a
which can hold 20
int
int *b=a;
Since a
is an array, a
evaluates to the base address of array a
(i.e. first element of a
). So b
is an int
pointer which points to the first element in array a
int *c=&a;
&a
means address of a
which is being assigned to int
pointer c
. But, this is not good as &a
is address of an array (not the base address of the array), so you should define c
as int **
(i.e. pointer to a pointer) or assign it to &a[0]
(i.e. the address of the first element of array a
). Since array is assigned contiguous memory, once you have the base address of the array, you can easily access the remaining elements of the array by doing *(b+i)
where i
is an index of the element you want to access.
However,
int a = 5;
int *b= a ; // WRONG -- b will not point to the address of variable a, but to the memory location 5, which is what you do not want
int *c = &a; //CORRECT