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?

Recommended Answers

All 6 Replies

struct car cobalt . car is not a pointer. I believe you want something like so, struct car cobalt[23] . In that case you could use the same notation as for regular arrays. For class object there is a special pointer operator cobalt->gas . Notice the "->" operator. For class objects you can use the arrow instead of the tedious (*cobalt).gas . In general though, avoid raw pointers.

(*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;

That access the first element. If you want to access the nth element you would do something like these :

Car car[25];
int n = 5; //for example
car[n].gas; //option # 1
(car + n)->gas; // option # 2
( *(car + n) ).gas; //option # 3

If possible go in that order. And if also possible go with std::vector instead of raw arrays/pointers.

(*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 both 4 bytes.

If your object is stored at adress 0x0000, it will consist of addresses 0x0000 to 0x000B (12 bytes).

wheels is from 0x0000 to 0x0003
gas is from 0x0004 to 0x0007
engineHP is from 0x0008 to 0x000B

If you have an array of 10 cars, that's 10 * 12 = 120 bytes. If you want to find the address of cars[4].engineHP and the array starts at 0x0010, that would be:

0x0010 + ((4 * sizeof(struct car)) + sizeof(int) + sizeof(float)) =
0x0010 + ((4 * 12) + 4 + 4) =
0x0010 + 56 =
0x0010 + 0x0038 =
address 0x0048.

That's how the internal calculation would go.

if i wanted to recreate this in pointer notation how would this work tho is what im asking lol..the above is what i was talking about! but im confused on how to do this pointer arithmetic using pointers? im guessing i would do the above but make sure the base address is casted to a pointer to a byte?

This 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()
{
    car cars[10];
    cars[5].engineHP = 340;
	
    cout << &(cars[5].engineHP)  << " " << cars[5].engineHP << "\n";	
    float engineHP = *((float*) (((char*)(cars + 5) + ENGINE_HP_OFFSET)));
    cout << engineHP << endl;
    return 0;
}
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.