Definition and reference of structural body arrays (IV.)

Source: Internet
Author: User
Tags define arrays definition printf reference

Pointer variables are very flexible and can point to any type of variable, if you define a pointer variable that points to a struct type change
, you can refer to the struct type variable by pointer.
7.3.1 the use of a variable pointing to a struct body type
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.
#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:
Student->name
Student->num
Student->birthday.year
Student->birthday.month
Student->birthday.day

7.3.2 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 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 the struct based on this type.
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.

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.