What are the main difference between array of pointers and pointer to array in C programming?
Often, we would see that an array is introduced as a pointer. Technically, that is not correct. Arrays are not pointer. It is like any other variable in C++. So what are the main difference between an array of pointers and the pointer to an array in C programming?
Let’s us use figurative analogy to explain.
It’s Christmas and you’ve got a gift somewhere inside your room. You also have a card that tells you where the gift is located. This is an analogy to the pointer and array. The card that points the gift is not the gift itself. The Christmas card is the pointer and the gift is the array.
Here is another scenario. The Christmas card lists several gifts and their location in your room. The card says something like, red wine is on the bed, a green scarf is under the bed, a blue shawl on top of the nightstand.
The card and the list it contains is like an array of pointers. The list is an array with pointers to different things inside your room.
Now, you may ask about the difference between the first and second scenario. And to explain further, the difference is that in the first scenario, you wrapped a gift inside a box and made a pointer to point to the box. In the next, you have a list of pointers to things that were in a box (in this case the box is the room.).
Do you see the difference? What are the main difference between an array of pointers to pointers to array in C? It is just like the difference between a card telling the location of a gift inside your room and a list pointing to different items in your room.
The second one, pointers to array in C programming is used to maintain a reference of things that may change their location over time. The things so to speak may not be directly related to each other. These things referred to would not make sense if placed in a single box.
The first one is used when you know that your gift or collection will remain same but you will need to pass around. This is another reference that means having different people access it.
Should you feel you have a better answer to the question, What are the main difference between array of pointers and pointer to array in C programming? Feel free to let us know.