"C Language" 21-structure

Source: Internet
Author: User

directory of this document
    • I. What is a structural body
    • II. definition of structural bodies
    • Third, the definition of structural variables
    • Four, the attention point of the structure body
    • V. Initialization of structures
    • Vi. use of structural bodies
    • VII. structure Array
    • VIII. structure as function parameter
    • Nine, pointer to the structure of the body

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore

C Language Core parts are said 7788, I believe you have the basic data Type C (char\int\float), arrays, pointers are very familiar with, today to learn the C language of another data type: structure. In iOS development, structs are frequently used data types that use the same frequency as pointers, so it is important to pay attention, but the usage is very simple.

Back to top one, what is a struct

* In the eighth lecture, we introduced the array in C, which is similar to other languages. When a whole is composed of multiple data, we can use an array to represent the whole, but the array has a feature: each element inside must be the same type of data.

* In practical applications, we usually need to make up a whole by different types of data, such as students, the whole can be composed of name, age, height and other data, these data have different types, names can be 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.

Back to top II. definition of structure 1. Definition form

The elements within the structure, that is, the constituent components, are generally referred to as "members".

The general definition of a struct is:

1 struct struct 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;     11};

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

2. For example

For example, we define a student

struct Student {    char *name;//name    int age;//ages    float height;//height};

The above defines a struct called student, which has a name, age, and HEIGHT3 members. Oh, see if there is a bit of object-oriented flavor, in fact, this is completely different from object-oriented, can only say that it feels a bit like.

Back to top III. definition of structural variables

The previous definition of a struct type named student does not define a struct variable, just like an int, but a type.

Next, define a struct variable, in many ways.

1. Define the structure type first, then define the variable
1 struct Student {2     char *name;3     int age;4};5 6 struct Student stu;

The 6th line defines a struct variable, with the variable named Stu. Structs and student are used in a connected.

2. Defining a variable at the same time as the struct type
struct Student {    char *name;    int age;} Stu

struct variable named Stu

3. Directly define struct type variable, omit type name
struct {    char *name;    int age;} Stu

struct variable named Stu

Back to top four, the structure of the attention point 1. Recursive definition of struct itself is not allowed

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

1 struct Student {2     int age;3     struct Student stu;4};

2. Structure body can contain other structures
1 struct Date {2     int year, 3     int month, 4     int day; 5}; 6  7 struct Student {8     char *name; 9     S Truct Date birthday;10};

Note Line 9th

3. Define the struct type, just describe the composition of the type, and do not allocate storage space for it, just as 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.

4. The memory space occupied by the struct variable is the sum of the memory occupied by its members, and the members are sorted in memory in the order defined

For example, the following student structure:

1 struct Student {2     char *name;//Name 3     int ages;//Age 4     float height;//Height 5};

In a 16-bit compiler environment, a student variable takes up a total of memory: 2 + 2 + 4 = 8 bytes.

Back to the top five, the initialization of the structure

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

such as initializing the 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};

Back to top vi. use of structures 1. The general operation of a struct variable is in the member unit, and 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//Access Stu of 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

2. If a member is also a struct variable, you can use the member operator continuously "." Access lowest level Member
1 struct Date {2      int year, 3      int month, 4      int day; 5}; 6  7 struct Student {8     char *name; 9     S Truct Date birthday;10};11 struct Student stu;13 stu.birthday.year = 1986;15 Stu.birthday.month = 9;16 stu.birthday . Day = 10;

Note the code after line 14th

3. The same type of structure variables can be assigned to the overall value
1 struct Student {2     char *name; 3     int age 4}; 5  6 struct Student stu1 = {"MJ", +}; 7  8//Assign STU1 directly to St U2 9 struct Student stu2 = stu1;10 One printf ("Age is%d", stu2.age);

Note Line 9th. The output is:

Back to top VII. structure Array 1. Define

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

2. 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.

Back to top VIII, struct as function parameter

When a struct variable is passed as a function parameter, it actually passes the value of all members, that is, the value one by one of the member in the argument is assigned to the corresponding parameter member. Therefore, changes to the parameters do not affect the arguments.

1 #include <stdio.h> 2  3//define a struct 4 struct Student {5     int age; 6}; 7  8 void test (struct Student stu) {9     printf ("Pre-modified parameter:%d \ n", stu.age),     age11 stu.age in the modified argument     = 10;12     printf ("Modified parameter:%d \ n", stu.age)}15 int main (int argc, const char * argv[]) {n     -struct Student stu = {30};19     printf ("Pre-modified Argument:%d \ n ", stu.age);     //Call the test function on     test (STU);     printf (" Modified argument:%d \ n ", Stu.age);     return 0;27}

* First defines a struct type in line 4th student

* A struct variable stu is defined in line 18th and passed to the test function as an argument in the 22nd row

The output is: The parameter is changed, but the argument has not changed

Back to the top nine, pointers to the structure

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

* Structure pointer variable definition form: struct structbody name * pointer variable name

* With 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     struct Student {6
   char *name; 7         int age; 8     }; 9     //define a struct variable one     struct Student stu = {"MJ", 27};12     // Define a pointer variable to struct     Student *p;15//Pointer to     struct variable stu17     p = &stu;18     /*20      There are 3 ways to access the members of the struct      */22     //mode 1: struct variable name. Member Name:     ("name=%s, age =%d \ n", Stu.name, stu.age); 24     //Mode 2: (* pointer variable name). Member Name:     printf ("name=%s, age =%d \ n", (*p). Name, (*p)     . Mode 3: Pointer variable name, member name     : printf ("name=%s, age =%d \ n", P->name, p->age);     return 0;32}

Output Result:

"C Language" 21-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.