C language programming study notes-user-created data types,

Source: Internet
Author: User

C language programming study notes-user-created data types,

C language programming learning notes-user-created data types.

9.1 define and use struct Variables

1. Definition: the C language allows users to build their own combinations of different types of data.

2. Form: struct name

{Member table column

Type name member name;}; // The member can belong to another struct type

3. Define struct type variables

(1) Declare the struct type and then define the variables of this type.

Struct studentstu1, stu2;

(2) define variables while declaring types

For example:

Struct student

{Int xx;

Char;} stu1, stu2;

4. Initialization and reference of Variables

(1) operator "." Priority MAX

? Example: sturuct student 1 = {. name = "gao bo"}; //. name indicates in 1 of the struct variable

Member 1. name. Other unspecified initialization values include 0, '\ 0', and pointer-'null '.

? Example: struct variable name. member name

Student. num // the value of a member that can reference a struct variable,

? Example: various operations can be performed.

Student1.score = student2.score

? Example:

Student 1 = Student 2 // struct variables of the same type can be assigned values to each other

? Example: struct variable member. The struct variable address can be referenced.

Scanf ("% d", & student1.num); // enter & student. num

Printf ("% o", & student1); // output the first address

Code:

# Include

Int main ()

{

StructStudent

{

Int num;

Char name [20];

Float score;

} Stu1, stu2;

Scanf ("% d % s % f", & stu1.num, stu1.name, & stu1.score );

Scanf ("% d % s % f", & stu2.num, stu2.name, & stu2.score );

Printf ("the max is ");

If (stu1.score> stu2.score)

{

Printf ("% d % s % f", stu1.num, stu1.name, stu1.score );

}

Elseif (stu1.score <stu2.score)

{

Printf ("% d % s % f", stu2.num, stu2.name, stu2.score );

}

Else

{

Printf ("% d % s % f", stu1.num, stu1.name, stu1.score );

Printf ("% d % s % f", stu2.num, stu2.name, stu2.score );

}

Return 0;

}

9.2 struct Array

1. Definition: struct name

{Member table} array name [array length ];

2. Initialization:

Array = {initialization table}

StructStudent stu [3] = {"", ","}

9.3 struct pointer

1. pointer to the struct variable

? Point to struct variables;

? Point to the operator "->"

Example: p-> number ==== (* p). numberp points to the number member in the struct

? Element // type in the array pointing to the struct must be the same

Example:

Structstudent * p1 // p1 can point to a variable or array element of the struct student type

3. pointer to the struct Array

? Struct arrays are continuously stored in the memory.

4. Use struct variables and pointers as function parameters

The struct variable value is passed to another parameter in three ways:

(1) Use a member of the struct variable as the parameter

(2) Using struct variables as real parameters

(3) the pointer to the struct variable is used as the real parameter, and the variable address is passed to the form parameter.

9.4 use pointers to process linked lists

1. Linked List: a structure for dynamic storage allocation.

Node:

? Actual data required by the user

? Address pointing to the next node (storing pointer variables)

Struct Student

{

Int num;

Int score;

Struct Student * next; // next pointer variable, pointing to Struct variable

}

2. Static linked list: All nodes are defined in the program and cannot be released after they are used up.

3. Dynamic Linked List: nodes are opened one by one, and the relationship between front and back links is established.

9.5 shared body type

1. Definition:

Several different debates share a memory structure and become a "shared body" structure.

2. General Form:

Union common body name union data

{{

Member column int I; char ch; float f;

} Variable table column;} a, B, c // defines the variable

3. Differences

? Struct variable: the memory length is the sum of the memory lengths of each member. Each member occupies its own memory unit.

? Shared body variable: the length of memory is equal to the length of the longest member.

5. Reference

? You cannot reference a park variable. You can only reference members in a shared body variable.

Example: a. I a. cha. f

6. Note

(1) Each time, only one of the members can be stored, rather than several

(2) You can use a shared body variable for initialization, but the initialization table can only contain one constant.

(3) The last assigned member replaces the original

(4) The address is the same as the address of each member & a. I & a. ch, & a. f

(5) You cannot assign values to variable names or obtain a value by referencing variable names.

9.6 use Enumeration type

1. Definition:

There are several possible values for a variable value. The so-called enumeration "lists possible values one by one '.

2. General Form:

Enum [enumeration name] {enumeration element list };

Enum Student {name, number, score };

3. Note:

(1) C compilation processes enumeration elements of the enumeration type according to constants and becomes enumeration constants.

(2) Each enumeration element represents an integer. The default values are 0, 1, 2, 3, and 4.

You can also create an enumstudent {num1 = 8, num2 = 9. num3 = 6} number,

(3) Comparison of enumeration Elements

9.7 declare a new type name with typdef

1. Replace the original type name with a new type name

? Role of typedef-you can use typedef to declare a new type name to replace an existing type name.

? Declaration method: the new type name of the typedef type name.

Example:

Typedef int integer; // specify integer as the type name. Same as int

IntI, j; is equivalent to integer I, j;

2. Name a simple type name instead of a complex type representation method.

(1) The new type name indicates the struct type.

Typedef struct

{

Int month;

Int day;

Int year;

} Date; // new type name Date, which can be used to define variables

Date birthday; // defines the struct type variable birthday

Date * p; // defines the struct Variable p, pointing to its struct type data

(2) name a new type name to represent the array type

(3) name a new type to indicate the pointer type.

(4) Name a new type name to indicate the pointer type to the function.

? Conclusion: Replace the variable name with the new type name by defining the variable, and add typedef to the front to declare

The new type name indicates the original 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.