Pointers to Structure

From WikiEducator
Jump to: navigation, search

Pointer and Structures:

A pointer variable can also hold the address of a structure variable like it holds the address of an ordinary variable. Observe the following structure definition:

 struct  Point
 {
  XCoord  ;
  YCoord ;
 } P1 , P2 ;  // P1 and P2  are structure variable.

Now if you want to point a pointer to these two variables P1 and P2, then we have to declare the pointer variables like :

 Point *f = &P1;
 Point *l = &P2;


One big question arises that how can we access the data members of a structure variable using these pointer variables? Now if you want to point a pointer to these two variables P1 and P2, then we have to declare the pointer variables like :

Point *f = &P1;
Point *l = &P2;

One big question arises that how can we access the data members of a structure variable using these pointer variables?

cout<< f->XCoord << f->YCoord ;

PointerPic14.JPG