Simple question
-
struct Data { int x; int y; }; Data* pData = new Data[30]; how do I access value x in item 5 in the Data array pData[5]->x = 3; // err pData[5].x = 3; //err /Mathias
-
struct Data { int x; int y; }; Data* pData = new Data[30]; how do I access value x in item 5 in the Data array pData[5]->x = 3; // err pData[5].x = 3; //err /Mathias
pData[5].x = 3; Since this is an array of structures, not pointers, this is the correct way to access it. (The brackets dereference the pData pointer.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
pData[5].x = 3; Since this is an array of structures, not pointers, this is the correct way to access it. (The brackets dereference the pData pointer.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Joe Woodbury wrote: The brackets dereference the pData pointer. Actually they don't. The dereferencing has to happen explicitly, as in:
*((Data *) pData[5]).x = 3;
PLEASE IGNORE THIS INCORRECT POST. I PROMISE NOT TO REPLY TO ANY CP POSTS UNTIL I'M REASONABLY AWAKE.
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
-
Joe Woodbury wrote: The brackets dereference the pData pointer. Actually they don't. The dereferencing has to happen explicitly, as in:
*((Data *) pData[5]).x = 3;
PLEASE IGNORE THIS INCORRECT POST. I PROMISE NOT TO REPLY TO ANY CP POSTS UNTIL I'M REASONABLY AWAKE.
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
-
No, the second compiles just fine. He allocating an array of structures, not an array of pointers to the structure. Tim Smith I'm going to patent thought. I have yet to see any prior art.
Gak! You're right of course! :-O /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com