结构体变量存放内存中,也有起始地址,定义一个变量来存放这个地址,那这个变量就是结构体指针;
typedef struct mydata{
int a1;
int a2;
int a3;
}mydata;
void CJgtzzView::OnDraw(CDC* pDC)
{
CJgtzzDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str1;
mydata b;
b.a1=101;b.a2=202;b.a3=303;
mydata * a = &b;
str1.Format("%d", a->a1);pDC->TextOut(50, 50, str1);
str1.Format("%d", a->a2);pDC->TextOut(100, 50, str1);
str1.Format("%d", a->a3);pDC->TextOut(150, 50, str1);
int *c = (int*)&b;
str1.Format("%d", *c);pDC->TextOut(50, 80, str1);
str1.Format("%d", *(c+1));pDC->TextOut(100, 80, str1);
str1.Format("%d", *(c+2));pDC->TextOut(150, 80, str1);
}
a是mydata类型的指针,a->a1、a->a2这样来访问成员;
后一种写法功能上是等价的;