Structure and shared body Summary

Source: Internet
Author: User

1. Concepts and Definitions of struct

1. struct Definition

Struct name

{

Data Type member name 1;

Data Type member name 1;

...

};

Here, the data type can be a composite type, for example, a struct type, that is, nesting;

 

2. struct variable definition

Struct name variable name 1, variable name 2, variable name N;

Or:

Struct name

{

Data Type member name 1;

Data Type member name 1;

...

} Struct variable 1, struct variable 2;

Or:

Struct

{

Data Type member name 1;

Data Type member name 1;

...

} Struct variable 1, struct variable 2;

Three methods, the third method, each time you want to define this type of struct variable, you must repeatedly write this

A piece of code;

 

After the struct is defined, the struct name is a new type.

The same variables;

 

3. struct variable reference

Only the members in the struct variables can be output separately. A struct variable cannot be used as a whole.

Output;

Reference of nested struct types:

Struct variable name. struct type member name. Embedded struct member name;

 

4. Initialization of struct Variables

Struct name variable name = {initialization data };

Or initialize it when it is defined:

Struct name

{

Data Type member name 1;

Data Type member name 1;

...

} Variable name = {initialization data };

 

2. struct Array

1. struct array definition example

Struct Stu

{

Int stu_nu;

Char name [20];

Float score;

};

Struct Stu student [5];

Or:

Struct Stu

{

Int stu_nu;

Char name [20];

Float score;

} Student [5];

Or:

Struct

{

Int stu_nu;

Char name [20];

Float score;

} Student [5];

It is the same as the three forms for defining struct variables, except that the struct variable defined each time is

Array; each student [I] is of the struct Stu type;

 

2. Initialization and reference of struct Arrays

Omitted;

 

3. struct and pointer

1. the pointer to the struct is defined in the same form as above;

Struct employees employee1, * P1;

Or:

Struct employees

{

...

} Employee1, * P1;

Or:

Struct

{

...

} Employee1, * P1;

Then, you can perform the following operations: p1 = & employee1; at this time, P1 points to the first address;

 

2. Use the struct variable pointer to reference struct members in two ways:

(* Pointer name of the struct variable). Member name;

For example: (* P1). Name; brackets cannot be omitted. The priority of. is the highest;

Or:

Pointer name of the struct variable-> member name;

For example, P1-> name;

Note: And-> have the highest priority;

Employee1.name;

(* P1). Name;

P1-> name;

Is equivalent;

 

3. struct array and pointer

Struct Stu

{

Int stu_nu;

Char name [20];

Float score;

} Student [5], * P;

You can

(1) Assign the starting address of the array student to P:

P = student; P points to student [0];

(2) You can also assign the address of other student elements to P.

P = & Student [3];

(3) Use a pointer to move P ++ to point P to different elements in the struct array;

 

4. struct as function parameters

1. struct variables are used as function parameters.

2. pointer to struct variable as function parameter

For more information, see the following example;

 

5. shared body (Consortium)

It can indicate that several different types of variables share the memory units of the same starting address. Its definition form is changed

Volume description, the reference method is similar to the structure, the essential difference is that the two storage methods are different: Structure

Each member variable occupies memory units of different starting addresses during storage, while each member of the shared body shares the same starting address during storage;

1. Definitions of shared objects

The three methods are the same as the struct.

Union data

{

Int I;

Float F;

Char ch;

};

Union data a, B, c;

Or:

Union data

{

Int I;

Float F;

Char ch;

} A, B, C;

Or:

Union

{

Int I;

Float F;

Char ch;

} A, B, C;

 

When defining a type, the system does not allocate a specific storage space to the shared object.

The system allocates space for the variables of the shared object type.

The size is the one with the largest space in the member item;

 

2. Reference of shared body Variables

Union data a, * P;

Its Reference Form:

A. I; A. F; A. Ch; or

P = & A; P-> = 5; P-> = 4.3; P-> = 'a ';

Know that & A, & A. I, & A. F, & A. Ch are the same value based on the storage features of the shared object;

C stipulates that the shared body variables cannot be used as function parameters or function return types;

At a certain time point, the storage space of the shared object can only store the data of one member.

When assigning values, pay attention to which member variable is stored during the last reference;

The following assignment is incorrect:

Union

{

Int I;

Float F;

Char ch;

} A = {5, 4.3, 'A'}; only one member can be assigned a value at a time, and the shared space stores the value of the last member;

In addition, the shared body can also be nested, or the shared body array can be defined, which is not described here, and the form is the same as the struct;

 

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.