A Pointer to an Array Versus an Array of Pointers

Examine these three declarations:

1:  Cat   FamilyOne[500]
2:  CAT * FamilyTwo[500];
3:  CAT * FamilyThree = new CAT[500];

FamilyOne is an array of 500 CATs. FamilyTwo is an array of 500 pointers to CATs. FamilyThree is a pointer to an array of 500 CATs.

The differences among these three code lines dramatically affect how these arrays operate. What is perhaps even more surprising is that FamilyThree is a variant of FamilyOne, but it is very different from FamilyTwo.

This raises the thorny issue of how pointers relate to arrays. FamilyThree is a pointer to an array. That is, the address in the pointer FamilyThree is the address of the first item in that array. This is exactly the case for FamilyOne; it is the actual address for the pointer FamilyThree.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.240