i understand int cars[23]; can be accessed using pointer notation such as...
int cars[10];
int *carz;
*(carz+1); // carz[1];
im curious as to what the equivalent pointer notation would be for a structure..for example
struct car
{
int wheels;
float gas;
float engineHP;
};
struct car cobalt;
cout << cobalt.engineHP << endl; // what's the equivalent of this in pointer notation?
lochnessmonster 0 Junior Poster
Recommended Answers
Jump to Post(*cobalt).gas how does this actually get converted by the compiler? how does it know what offset in the struct to go to?
it seems like there should be some kind of internal calculation that is done...for example
(*cobalt)+3;
The entire struct has size 12 bytes, assuming ints and float are …
Jump to PostThis seems to do the trick. Perhaps it could be simplified.
#include <iostream> using namespace std; struct car { int wheels; float gas; float engineHP; }; const int WHEELS_OFFSET = 0; const int GAS_OFFSET = WHEELS_OFFSET + sizeof(int); const int ENGINE_HP_OFFSET = GAS_OFFSET + sizeof(float); int main() …
All 6 Replies
mrnutty 761 Senior Poster
lochnessmonster 0 Junior Poster
mrnutty 761 Senior Poster
VernonDozier 2,218 Posting Expert Featured Poster
lochnessmonster 0 Junior Poster
VernonDozier 2,218 Posting Expert Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.