Many times I have seen many people are getting confused between “Array of Pointer(s)” and “Pointer to an Array”.
So, today I have decided to put an excerpt on this interesting topic.
Please leave your comment, Which will help me to improve this article.
Pointer To An Array As the name suggests; a pointer pointsto an array. So a pointer to an array type is called an array pointer.
For Example:- int (*PTR_TO_ARRAY) = NULL;
Let’s analyze the above example:-
When we declare pointer to an array type, we must need to use parentheses.
So in the above example, we can see the pointerPTR_TO_ARRAYis pointing to an array of ten elements with type of int. The pointer to an array of ten elements are initialized with NULL.
Array of Pointers
As the name suggests;
an array contains certain number of pointers to specific data types.
Let’s see the below example.
For Example:- int* ARRAY_OF_PTRS ;
We can see from the above statement that, ARRAY_OF_PTRS is
an array often (10)pointers to integers.
Let’s analyze this in below program
int Rasmi =1, Ranjan=2, Nayak=3;
array = &Rasmi; /*Array of 0 stores the address of Rasmi*/
array = &Ranjan; /*Array of1 stores the address of Ranjan*/
array = &Nayak; /*Array of2 stores the address of Nayak*/
printf(“The value of %d = %d, address is %u”,i,*(array[i]), array[i]);
Run the program to see the output or scroll down to see the output
Thevalueof0 =1,addressissome address (Varies from System to system)
Thevalueof1 =2,addressissome address (Varies from System to system)Thevalueof2 =3,addressissome address (Varies from System to system)
When we assign array = &Rasmi;
It means the address(0xSomething) of Rasmi gets stored in array.
At the mean time if we want to access the value stored in Rasmi (variable Rasmi assigned to 1), how can we do that with pointer?
See; arrar stores the address of variable Rasmi, that means the pointer *array
points to the value which is stored inside the addressof Rasmi.
Please go through the below links
Book(s) can be referred:
Practical C Programming by Steve Oualline, O’reilly Publication
I hope this article will be helpful for you.
Please do writeyour commentsand suggestions (to make the correction in this article).
Please check out my other knols. Which might behelpful for you.
Thanks A Lot