struct-Body pointer

Source: Internet
Author: User

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

#include <stdlib.h>/* Use M a l l o C () required */
struct Data/* define struct */
{
int day,month,year;
} ;
struct STU/* define struct body */
{
Char name[20];
Long num;
struct data birthday; /nested * Set of struct type members */
} ;
Main ()
{
struct Stu *student; Fixed//semantic struct type pointer */
Student=malloc (sizeof (struct stu)); Assign a secure address to/from the * PIN variable */
printf ("Input name,number,year,month,day:/n");
scanf ("%s", student->name); * * Enter student's 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");
/* Print out values for each member item */
printf ("%20s%10ld%10d//%d//%d/n", Student->name,student->num,
Student->birthday.year,student->birthday.month,
Student->birthday.day);
}


Using struct type pointers in your program to refer to members of struct variables, you need to pass the function malloc () provided by C to
The pointer assigns a secure address. The function sizeof () returns a value that calculates the number of bytes of memory for a given data type. The pointer means
The Member forms are:


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.
A pointer to a struct type that can point to an element of an array, or to an array, to be distinguished when used.
[Example 7-3] In example 7-2 defines the structure of the type, based on this type and then define the structure of the body array and pointer to the struct type .


struct data
{
Intday,month,year;
};
struct STU/* Define structure body */
{
Char name[20];
Long num;
struct data birthday;/The struct type member of the nested * set * *
};


struct stustudent[4],*p;///semantic struct array and pointer to struct type */
As P=student, the pointer p points to the struct array student.
P is a pointer to an array of one-dimensional structures that can be referenced in three ways.
1) Address law
Both Student+i and p+i represent the addresses of the element I elements of the array, and the members of the array elements 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].
2) Pointer method
If p points to an element of an array, then p++ points to its subsequent elements.
3) array notation for pointers
If p=student, we say that pointer p to array student,p[i] represents the element I of the array, and its effect is
Student[i] equivalent. A reference to an array member is described as: P[i].name, P[i].num, and so on.
[Example 7-4] The use of pointer variables to a struct array.


structdata/* define struct type */
{
Intday,month,year;
};
structstu/* define struct type */
{
Char name[20];
Long num;
struct data birthday;
};
Main ()
{int i;
structstu*p,student[4]={{"Liying", 1,1978,5,23},{"wangping", 2,1979,3,14},
{"Libo", 3,1980,5,6},{"Xuyan", 4,1980,4,21}};
/* Define struct array and initialize */
P=student;/* assigns the first address of the array to the pointer p,p points to a one-dimensional array student*/
printf ("/n1----outputname,number,year,month,day/n");
for (i=0;i<4;i++)/* Use the pointer method to output the members of the array element */
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);
}

struct-Body pointer

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.