Dark Horse programmer -09-Structural body

Source: Internet
Author: User

l What is a struct

In practical applications, we usually need to make up a whole by different types of data, such as students, the whole can be composed by name, age, height and other data, the data have different types, the name can be a string type, age can be integer, height can be floating point type. For this reason, the C language specifically provides a construction type to address the above problem, which is the struct, which allows the internal elements to be of different types.

L Definition of structural body

The general definition of a struct is:

1 struct structural body name {

2

3 Type name 1 member name 1;

4

5 Type Name 2 member name 2;

6

7 ...

8

9 type name n member name N;

10

11};

A struct is a keyword, a symbol of a struct type.

L Definition of structural variables

U define struct type first, then define variable

struct variable named Stu

1 struct Student {

2 Char *name;

3 int age;

4};

5

6 struct Student stu;

U define variables at the same time as the struct body

struct Student {

Char *name;

int age;

} Stu;

struct variable named Stu

U directly define struct type variable omit type name

struct {

Char *name;

int age;

} Stu;

struct variable named Stu

L Structural Body Note points

U does not allow the struct itself to be defined recursively

The following procedure is wrong, take note of line 3rd

1 struct Student {

2 int age;

3 struct Student stu;

4};

U structure body can contain other structures

1 struct Date {

2 int year;

3 intmonth;

4 int day;

5};

6

7 struct Student {

8 Char*name;

9 struct Datebirthday;

10};

Note Line 9th

U defines a struct type, which simply illustrates the composition of the type, and does not allocate storage space to it, as if the system does not allocate space for the int type itself. The system allocates storage to this variable only if it is defined as a variable of the struct type

1 struct Student {

2 Char *name;

3 int age;

4};

5

6 struct Student stu;

Row 1th to 4th does not allocate storage space, and the system allocates storage to the STU variable when it executes to row 6th.

• Initialization of the structural body

You place the initial values of each member sequentially in a pair of curly braces {}, separated by commas and corresponding to the assignment.

U like initializing student structure variable stu

1 struct Student {

2 Char *name;

3 int age;

4};

5

6 struct Student stu = {"MJ", 27};

Initialization assignments can only be initialized at the same time as variables are defined, and the definitions of the initialization assignments and variables cannot be separated, and the following procedure is incorrect:

struct Student stu;

Stu = {"MJ", 27};

l Use of structure body

U general operations on struct variables are in the member units, the general form of the reference is: struct variable name. Member Name

1 struct Student {

2 Char *name;

3 int age;

4};

5

6 struct Student stu;

7

8//Visit Stu's age member

9 Stu.age = 27;

The 9th Line assigns the age member of the struct to a value. "." Called the member operator, which has the highest precedence in all operators

U the same type of struct variable can be assigned as a whole

1 struct Student {

2 char*name;

3 int age;

4};

5

6 struct Student stu1 = {"MJ", 27};

7

8//Assign STU1 directly to STU2

9 struct Student stu2 = stu1;

10

printf ("Age is%d", stu2.age);

Note Line 9th. The output is:

L Structure Array

U definition

As with struct variables, there are 3 ways to define a struct array

struct Student {

Char *name;

int age;

};

struct Student stu[5]; Definition 1

struct Student {

Char *name;

int age;

} Stu[5]; Definition 2

struct {

Char *name;

int age;

} Stu[5]; Definition 3

The above 3 methods define a struct array with a variable named Stu, and the number of elements in the array is 5

U initialization

struct {

Char *name;

int age;

} Stu[2] = {{"MJ", +}, {"JJ", 30}};

You can also use an array subscript to access each structure element, which is the same as normal array usage.

L pointer to struct body

U each struct variable has its own storage space and address, so pointers can also point to struct variables

u struct pointer variable definition form:struct struct name * pointer variable name

You have pointers to structs, there are 3 ways to access struct members

· struct variable name. Member Name

· (* pointer variable name). Member Name

· Pointer variable name, member name

1 #include <stdio.h>

2

3 int main (int argc,const char * argv[]) {

4//define a struct type

5 Structstudent {

6 char*name;

7 int age;

8};

9

10//define a struct-body variable

One struct Student stu = {"MJ", 27};

12

13//Define a pointer variable pointing to the struct body

Student *p of the struct;

15

16//Pointer to struct variable stu

p= &stu;

18

19/*

20 The members of the struct can be accessed in 3 ways at this time

21 */

22//Mode 1: struct variable name. Member Name

printf ("name=%s, age =%d \ n", Stu.name, Stu.age);

24

25//Mode 2: (* pointer variable name). Member Name

-printf ("name=%s, age =%d \ n", (*p). Name, (*p). age);

27

28//Mode 3: Pointer variable name, member name

printf ("name=%s, age =%d \ n", p->name,p->age);

30

return 0;

32}

Dark Horse programmer -09-Structural body

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.