IOSDAY06C language Structure

Source: Internet
Author: User

1, the structure of the overview

in the C language, Structural Body (struct) refers to a data structure that is a C-language the type of construction in the word One of them.

  in real-world applications, we often need different types of data to form astudents as a whole can be made up of name, age, height, etc.all of these data have different types, names can be stringstype, age can be integer, height can be floating point type. The C language provides a structure to address our needs, which allows the internalelement of the department is Different types of .   a struct is a custom data type used to hold the same type of data or different types of data

2, the definition of structural body

struct struct body name {
Member variable 1;
Member variable 2;
...
};

1 structStudent {2     3     //member variables for structs4     Charname[ -];//name5     CharGender//Sex6     intAge//Age7     DoubleScore;//Achievements8     9};//semicolons cannot be omitted

3, the definition of structural variables

First: define the struct type, then define the variable  This is defined in the same way as normal data type variables;struct struct body name variable name; Note: The struct struct name is the data type, and the simple struct name does not count as data type.
1 struct  2     char name[3      int   4     float5};//semicolons cannot omit 6struct teacher Englishteacher;
   the second way: define a   variable at the same time as the struct type
1 struct Teacher {2     Char name[]; 3     int Age ; 4     float height; 5  } mathteacher;

Where Mathteacher is the variable name.

Third Way: directly define struct type variable, omit type name  
1 struct {2     Char name[]; 3     int Age ; 4     float height; 5  } chineseteacher;

Where Chineseteacher is the variable name, this struct is called an anonymous struct.
4, the redefinition of the structural body 

There are two ways to redefine a struct:1)thealready haveThe structure is redefined: 1 typedef struct teacher teacher;

2)    when creating a struct , redefine it:

1 struct Teacher {2     Char name[]; 3     int Age ; 4     float height; 5  } Teacher;

5. Members of struct variables

1) initialization of struct variable members: using {...} Assigning an initial value to a constructed type

1 Teacher 中文版Teacher = {"ZF", 1.70f};

Note: The value type of the assignment and the type one by one of the member variable correspond

2) Assign value: The 1> first finds the struct variable, and then accesses the member of the variable through the "." operator, and the simple data type can be directly assigned
1 TT 2  - 3 1.77f

2> structure and structure can be directly assigned value

      Note: Arrays cannot be directly assigned to a value

Tip: You can assign a value directly by using the array as a member of the struct.       

6. Calculation of the amount of memory occupied by the structure

1) Maximum allocation (maximum length of memory allocated)

2) memory alignment ( The corresponding type occupies a multiple of the bytes)

1 struct Student {2     Char name[]; // Memory 0-20 3     char gender;    // Memory 4     int age;        // Memory 24-27 5     double score;   // Memory 32-39 6 }; 

Graphic:


7. Structure nesting

1 structBirthday {2     intYear ;3     intmonth;4     intDay ;5 };6typedefstructbirthday birthday;7 8 structPerson {9     Charname[ -];Ten     Chargender; One Birthday bir; A }; -typedefstructperson person;
when a struct is nested, care should be taken to avoid using the struct itself , which is wrong in the following notation:
1 struct Person {2     Char name[]; 3     struct Person per; 4  };       

8. Structure array

  The type of the array is the struct type, which is the same as an array of other data types, and can be referenced in the iosday04c language array

IOSDAY06C language Structure

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.