C language - Data Structure - structure

First, the definition of the structure of the

Array (the Array) is a collection of data of the same type. But in the actual programming process, we often need a different set of types of data, such as student information for the registration form, a string name, student number is an integer, age integer, character study groups where a score of decimals because different types of data, obviously can not use an array to store.

Structure (StructEntryTable) may store a set of different types of data. Which is the aggregate data type in C language (aggregate data type) of a class. Structure may be declared as variables, arrays, or other pointer, to achieve more complex data structures. But also a collection of some of the elements of the structure, these elements are called members of the structure (member), and these members can be of different types, members of the general access by name.

It is defined in the form of structure:

struct structure name {
    Variables included in the structure or array
};

E.g:

struct STU {
     char * name;   // Name 
    int NUM;   // Student ID 
    int Age;   // Age 
    char Group;   // where the study group 
};

Definition of the structure variables:

struct stu stu1,stu2;

 In the definition of the structure or at the same time define the variables:

struct STU {
     char * name;   // Name 
    int NUM;   // Student ID 
    int Age;   // Age 
    char Group;   // where the study group 
} stu1, stu2;

 Commonly using typedef for the struct STU alias:

typedef struct STU {
     char * name;   // Name 
    int NUM;   // Student ID 
    int Age;   // Age 
    char Group;   // where the study group 
} Student; 
Student STU1, STU2;

To structure variable assignment:

    stu1.name = "Tom";
    stu1.num = 12 ;
    stu1.age = 18;
    stu1.group = 'A';
    stu1.score = 136.5;

Second, the definition of the structure of the array

// Assignment 
struct
STU STU1 [ . 5 ]; // or STU1 Student [. 5];
// Assignment
class [ . 5 ] = { {"Li ping", 5, 18, 'C', 145.0}, {"Zhang ping", 4, 19, 'A', 130.5}, {"He fang", 1, 18, 'A', 148.5}, {"Cheng ling", 2, 17, 'F', 139.0}, {"Wang ming", 3, 17, 'B', 144.5} }

Using an array of structures, e.g.,

Wang ming get results: stu1 [4] .score; 

Li ping modified study groups: class [0] .group = 'B';

Third, the pointer to the structure

When a pointer variable points to the structure , we call it the structure pointer. C language structure pointer defined generally in the form:

struct structure name * variable name;

Note that the variable name structures and array name is different array name will be converted to an array pointer in an expression, and the structure variable names do not, regardless of any expression that represents the entire collection is itself, in order to achieve address of the structure variable must be preceded by& , so to pstu only writing assignments:

struct stu *pstu = &stu1;

Get structure member:. higher priority than *, (*pointer)on both sides of the brackets can not be less.

(*pointer).memberName

Or: ->is a new operator, can be obtained directly through the structure member structure pointer;

pointer->memberName

Structure pointer parameter as a function of:

Representatives of the entire structure of the entire set of variables set itself, when delivered as parameters, that is, all the members, rather than the array as the compiler is converted into a pointer . If more structure members, especially the members of an array, time and space overhead transmission of large, affecting the efficiency of the program. So the best way is to use the structure pointer, then the arguments passed to the parameter is only one address, very fast.

Note: (using an array of pointers structure does not require additional front structure array variable & )

#include <stdio.h>

struct STU {
     char * name;   // name 
    int NUM;   // Student ID 
    int Age;   // Age 
    char Group;   // where Panel 
    a float Score;   // score 
} stus [] = {
    {"Zhou ping", 5, 18, 'C', 145.0},
    {"Zhang ping", 4, 19, 'A', 130.5},
    {"Liu fang", 1, 18, 'A', 148.5},
    {"Cheng ling", 2, 17, 'F', 139.0},
    {"Wang ming", 3, 17, 'B', 144.5}
}, *ps;

int main () {
     // find the array length 
    int len = the sizeof (stus) / the sizeof ( struct STU);
    printf ( " Name \ T \ tnum \ stage \ tGroup \ tScore \ r \ n " );
    for (ps = heat; ps <heat + len; ps ++ ) {
        printf("%s\t%d\t%d\t%c\t%.1f\n", ps->name, ps->num, ps->age, ps->group, ps->score);
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lemonzhang/p/12301971.html