Deep analysis of the definition and reference of structure pointer in C language _c language

Source: Internet
Author: User

Pointing to the use of struct-type variables
Let's first define the structure:
struct STU
{
Char name[20];
Long number;
float score[4];
} ;
To define a pointer variable that points to a struct type variable:
struct Stu *p1, *p2;
Define the pointer variable p 1, p 2, pointing to the struct type variable respectively. The reference form is: pointer variable → member;
[Example 7-2] The correct use of a variable pointing to a struct type. enter a member of a struct type variable and output it.

Copy Code code as follows:

#include <stdlib.h> * * Use M a l l o C () need */
struct data/* Definition structure */
{
int day,month,year;
} ;
struct STU/* Defines the structure body */
{
Char name[20];
Long num;
struct data birthday; /embedded * Set of structural body type members */
} ;
Main ()/* defines m a i n () function */
{
struct Stu *student; Fixed///struct body type pointer * *
Student=malloc (sizeof (struct stu)); Assign a secure address to/to a PIN variable
printf ("Input name,number,year,month,day:/n");
scanf ("%s", student->name); Enter the student name, school number, date of birth * *
scanf ("%ld", &student->num);
scanf ("%d%d%d", &student->birthday.year,&student->birthday.month,
&student->birthday.day);
printf ("/noutputname,number,year,month,day/n");
/* The value of the printed output member items
printf ("%20s%10ld%10d//%d//%d/n", Student->name,student->num,
Student->birthday.year,student->birthday.month,
Student->birthday.day);
}

A member of a struct variable is referenced using a struct type pointer in a program, and a function malloc () provided by C is required to
The pointer assigns a secure address. The function sizeof () return value is the number of bytes of memory that are computed for the given data type. The point of the pointer
Each member form is:
Copy Code code as follows:

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

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 the previous course describes very clearly.
A pointer that defines the type of struct that can point to elements of an array, or to an array, to be distinguished when used.
[Example 7-3] defines the structure body type in example 7-2, and then defines the structure body array and pointers to the type of struct body based on this type
Copy Code code as follows:

struct data
{
Intday,month,year;
};
struct STU/* definition structure Body * *
{
Char name[20];
Long num;
struct Data birthday;/nested * Member of struct type members */
};

struct STUSTUDENT[4],*P/fixed///structure body array and pointer to struct type
As a p=student, the pointer p points to the structural body array student.
p is a pointer to an array of one-dimensional structures, which can be referenced in three ways.
1) Address law
Both Student+i and p+i represent the address of the first element of the array, and the elements of the array are referenced in the following form:
(Student+i)->name, (Student+i)->num and (P+i)->name, (p+i)->num, etc. Student+i and P+i
Same as &student[i] meaning.
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.
Copy Code code as follows:

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.