Structure Type of C Summary, C Summary

Source: Internet
Author: User

Structure Type of C Summary, C Summary

Structure Type of C Summary

@ Brief diamond game

(1) Enumeration type
Enum COLOR {BLACK, RED, BLUE };
// Declare a new data type. The values are 0, 1, and 2 respectively, but they are represented by BLACK/RED/BLUE.
You can also skip this step.
Enum COLOR {RED = 1, YELLO, BLUE}; // The value is 1, 2, 3
The essence of enum is int type.
Application: it can represent a column of const int, which is used as the symbolic quantity.

(2) Structure Type
The struct type is a composite data type and a variable, which contains some data.
1> struct Declaration
Method 1: declare struct

Struct point {// create struct type int x; int y ;}

Struct point P1; // declare the point type variable P1

Method 2: simply want two variables, instead of declaring this type of structure

struct{int x;int y;}p1,p2;

Method 3: perform two things at the same time. One is to create a struct to change the face type, and the other is to declare two struct variables.

struct point{int x;int y;}p1,p2;

2> struct variable assignment

Struct point p1 = {12, 32 };
Struct point p2 = {, y = 10}; // The default value is 0.
Note: keep up with struct.
3> access and use of structure members
Use the. domain operator
P1.x = 2;
Z = p1.y;
You can assign a value to the whole, for example, P1 = (struct point) {4, 5 };

4> the value passed in the function is not the address. If we want to change the variable, we should input the address.
& P1
P1-> x indicates the member variable x in P1


Thought: In a function, it is better to pass in a struct than to pass through the pointer of the struct, and then use it in the function body.

5> structure array
Point P1 [2] = {1, 2}, {2, 3}; // two coordinate P1 [0] = {1, 2} P2 [1] = {2, 3}

print(point *p1){printf("x=%d,y=%d",p1->x,p1->y);}

(3) Custom Data Types

The Custom Data Type created by struct must keep up with struct when declaring variables.
Typedef provides a data type alias
For example
Typedef int LENGTH // indicates that an alias for int Is LENGTH.
You can use it later.
LENGH l1, l2;
So we can use it to do this.

typedef struct APOINT{    int x;    int y;} POINT;

It is equivalent

Typedef <---> POINT // indicates that the POINT variable type is created for the struct type.
You can do this later.
POINT P1 = {1, 2 };


(4) Federated Data Types
Representation: union
Example:
Union POINT {
Char I;
Int y;
}
After the shared object declaration, the actual memory size is the maximum size of the contained variables. In the above program, it should be the maximum size of the int, so it is the size of four bytes.
The memory is arranged in this way.
I 【]------------
Y 【]【]【]【]
// [] Indicates memory possession ---- memory does not possess
They share a piece of memory.
Application: You can use the shared body to split long variables into four char or binary values for observation.

 

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.