Structure of a C + + custom data type

Source: Internet
Author: User

Structure

A struct is a collection of several variables of a fixed number of different data types . In fact, structs can also be collections of the same variables .

Defining a structure variable should first define the structure, and any one of the structural variables has some structural pattern. Structure pattern is the form of structure variable, and a structure pattern can define several structure variables.

definition format for structural patterns :

struct < struct name >

{

< several member descriptions > (give name and type of each member)

};

structure variables are defined in the format:

struct < struct name > < structure variable Name table >

For example:

struct student

{

char name;

int age;

char sex;

struct date birthday; (structure variable)

}c1,*p,a[12];

struct date

{

int year,month,day;

};

A structural variable of a struct can also be a member of another struct. The members of the structure are different data types. A struct variable can be a generic struct variable, a pointer to a struct variable, or an array of structures. (The structure array is an array of array elements as structural variables.)

Defining a struct variable can be defined separately after the structure pattern is defined, or it can be defined while defining the structure pattern. The structural variables described above are defined as structural variables at the same time as the structure pattern is defined, to see separately defined:

struct CARD

{

int A;

Char b;

};

struct card s1,s2,*pi,d[12];

representation of struct variable members;

The representation of a member of a generic struct variable is by using the operator "." Connect the. struct variable name to the member name:

< struct variable name >.< member name > s1.a

A member of a pointer to a struct variable is represented by using the operator "-" to concatenate the pointer name and member name of the struct variable:

< struct variable pointer name >->< member name > pi->b

A member of a pointer to a struct variable can also be represented by:

(*< struct variable pointer name >) .< member name > (*PI). b

assignment of the structure variable:

1 structure variable assignment initial value:

The method of assigning an initial value to a struct variable is to use the initializer table, and the order of the data items given in the initial value table should be the same as the order of the members defined.

General structure variable assignment initial value: struct card c1={1, ' s '};

pointer to struct variable assigns the initial value: struct card *pi=&c1; (&C1 represents the address value of the structure variable C1)

The structure array assigns the initial value; struct card s[2]={{1, ' s '},{2, ' s '};

2 Structure Variable assignment:

General structure variable assignment: struct.old=23;struct.birthday.day=15;

pointer to struct variable pointers assignment: pi->birthday.year=2017;

Structure array assignment; S[2].name= "Wang Yan";

You can also assign a struct variable to another structure variable in the same structure mode as the two structure variables.

struct card c1={1, ' s '},c2;

C2=C1;

The operation of the structure variable:

The operation of structural variables mainly refers to the operation of the members of the structure variables, and the structure variables are only assigned on the whole.

Write a program:

#include <iostream.h>struct student{char *name;long stu_no;float math;float 中文版;}; void Main () {static struct student s1={"Ma Jing", 99012,89.0,78.5},                  s2={"Lu ping", 99023,90.0,85.5};float m1,m2;m1= ( S1.math+s1.english)/2;    M2= (s2.math+s2.english)/2;cout<<s1.name<< ' \ t ' <<m1<<endl;    cout<<s2.name<< ' \ t ' <<m2<<endl;}

Result of output: Ma jing 83.75

Lu Ping 87.75

Structure of a C + + custom data type

Related Article

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.