2) Pointer method
If p points to an element of an array, p++ points to its subsequent elements.
3 array notation of pointers
If p=student, we say that pointer p points to array Student,p[i] represents the first element of an array, with the effect
Student[i] equivalent. A reference to an array member is described as: P[i].name, P[i].num, and so on.
[Example 7-4] uses a pointer variable that points to an array of struct bodies.
STRUCTDATA/* definition Structure Type * *
{
Intday,month,year;
};
STRUCTSTU/* definition Structure Type * *
{
Char name[20];
Long num;
struct data birthday;
};
Main ()
{inti;
structstu*p,student[4]={{"Liying", 1,1978,5,23},{"wangping", 2,1979,3,14},
{"Libo", 3,1980,5,6},{"Xuyan", 4,1980,4,21}};
/* Defines an array of structures and initializes it.
P=student;/* assigns the first address of the array to the pointer p,p a one-dimensional array student*/
printf ("\n1----outputname,number,year,month,day\n");
for (i=0;i<4;i++)///* The members of the array element are output by the pointer method */
printf ("%20s%10ld%10d//%d//%d\n", (P+i)->name, (p+i)->num,
(P+i)->birthday.year, (p+i)->birthday.month,
(p+i)->birthday.day);
}