Confused with pointer to structure

My question is , for example we have a structure like this :
1
2
3
4
5
struct CandyBar {
	std::string brand;
	double weight;
	unsigned short numOfCalo;
};

and we declare a pointer to it:
1
2
3
4
CandyBar * pt = new CandyBar;
	pt->brand = "hello" ;
	pt->weight = 2.4;
	pt->numOfCalo = 421;

So when printing out :
1
2
3
std::cout << pt;
std::cout << pt+1;
std::cout << pt+2; 

Is pt+1 and pt+2 the address of weight and numOfCalo or it is st # ? I'm not sure about it?
pt+1 will print the value of ( address pointed by pt + sizeof(CandyBar) ) which is some location in memory not related to anything in your code.
pt->brand is how you set that value, so pt->brand is how you can read that value.

std::cout << pt->brand;

The address of weight, if you really want it, can be found like this:

&(pt->weight)

Last edited on
I understood.thank you guys !
Topic archived. No new replies allowed.