The definition and reference of structure pointer in C language are deeply analyzed

  • 2020-04-01 23:35:36
  • OfStack

Points to the use of struct type variables
First let's define the structure:
Struct stu
{
Char name [20].
Long number;
Float score [4];
};
Redefines the pointer to the struct type variable:
Struct stu *p1, *p2;
Define pointer variables p 1 and p 2 to point to struct type variables, respectively. The form of reference is: pointer variable - member;
[example 7-2] correct use of variables that point to struct types. Input a member of a struct type variable and output.


#include <stdlib.h> /* use m a l l o c ( )  Need to be * /
struct data / * Defining structure * /
{
int day,month,year;
} ;
struct stu /* Defining structure * /
{
char name[20];
long num;
struct data birthday; / embedded * Set of struct type members */
} ;
main() /* define m a i n ( )  function * /
{
struct stu *student;  set 
student=malloc(sizeof(struct stu));  for / Refers to the *  Pin variables assign secure addresses */
printf("Input name,number,year,month,day:/n");
scanf("%s",student->name);  Lose, 
scanf("%ld",&student->num);
scanf("%d%d%d",&student->birthday.year,&student->birthday.month,
&student->birthday.day);
printf("/nOutputname,number,year,month,day/n");

printf("%20s%10ld%10d//%d//%d/n",student->name,student->num,
student->birthday.year,student->birthday.month,
student->birthday.day);
}

A struct type pointer is used to refer to a member of a struct variable in a program, which needs to be
A pointer assigns a secure address. The return value of the function sizeof() is the number of bytes in memory for a given data type. Pointers
Each member form is:

student->name
student->num
student->birthday.year
student->birthday.month
student->birthday.day

The use of Pointers to arrays of struct types
Defines an array of struct types whose array name is the first address of the array, as was made clear in the previous lecture.
A pointer to a struct type that can point to either an element or an array is used to distinguish between them.
[example 7-3] in example 7-2, a struct type is defined, from which an array of structs and a pointer to the struct type are defined .

struct data
{
intday,month,year;
};
struct stu
{
char name[20];
long num;
struct data birthday;/ embedded * Set of struct type members */
};

Struct stustudent [4], * p; set
If I make p=student, then the pointer p points to the struct array student.
P is a pointer to a one-dimensional array of structs. There are three ways to reference an array element.
1) address
Both student+ I and p+ I represent the address of the ith element of the array. The reference form of each member of the array element is:
(student + I) - > The name, (student + I) - > Num and (p + I) > Name, (p+ I) - > Num, etc. Student + I and p + I
It has the same meaning as &student[I].
2) pointer
If p points to an element of the array, p++ points to its subsequent element.
3) array representation of pointer
If p=student, we say that the pointer p points to the array student, p[I] represents the ith element of the array, and the effect is
Student [I]. A reference to a member of a logarithm group is described as :p[I].name, p[I].num, etc.
[example 7-4] the use of pointer variables to an array of structs.

structdata
{
intday,month,year;
};
structstu
{
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}};

p=student;
printf("/n1----Outputname,number,year,month,day/n");
for(i=0;i<4;i++)
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);
}


Related articles: