Analyze the definition and usage of structure

  • 2020-04-01 23:35:41
  • OfStack

Definition of structure
The general form of a structure is defined as:
A struct structure
{
Members of the columns of the table
}
A member table consists of several members, each of which is a component of the structure.
Each member must also be typed.
Such as:


struct stu 
{ 
int num; 
char name[20]; 
int age; 
}

Description of structure type variables
The struct definition does not define a variable, it defines a type The data type , which you define, can be used in the same way as the language's own simple data types (such as int).
The structure itself does not make memory available as data; what is actually stored in memory as data is the variables defined by the structure.
How much memory does a struct variable take up? This is determined by the definition of the structure type, and you can imagine that in order to store each member of the structure at the same time, the storage size of the structure variable should be the total storage space of all the components.
There are three ways to describe a structure variable. Take the stu defined above as an example to illustrate.
1. Define the structure first, then explain the structure variables. Such as:

struct stu 
{ 
int num; 
char name[20]; 
int age; 
}; 
struct stu boy1,boy2; 

The two variables boy1 and boy2 are stu structure types.
You can also use the macro definition to make a symbolic constant to represent a structure type, for example:

#define STU struct stu 
STU 
{ 
int num; 
char name[20]; 
int age; 
}; 
STU boy1,boy2;  

2. Explain the structure variables while defining the structure type. Such as:

struct stu 
{ 
int num; 
char name[20]; 
int age; 
}boy1,boy2; 

3. Directly explain structural variables.
Such as:

struct 
{ 
int num; 
char name[20]; 
int age; 
}boy1,boy2;  

The difference between the third method and the second method is that the third method omits the structure name and gives the structure variable directly.
Note that once boy1 and boy2 are of type stu, you can assign values to each member of the two variables.
In the stu structure definition above, all members are primitive data types or array types. Members can also be a structure, that is, a nested structure.
Such as:

struct date{ 
int month; 
int day; 
int year; 
} 
struct{ 
int num; 
char name[20]; 
struct date birthday; 
}boy1,boy2; 

The general form of using structure variable members is:
Structure variable name. Member name
Such as:
Boy1. The num

If the member itself is a structure, the lowest member must be found level by level to be used.
For example: boy1. Birthday. The month
That is, the member of the month in which the first person is born can be used in the program alone, exactly the same as a normal variable.  
Initialization of structure variables: similar to the initialization of multidimensional arrays. Assignment of a structure variable
You can assign each member of a struct variable one by one, much like an array, which is an element by element assignment (for arrays, you have no choice but to do this unless you initialize it). Unlike arrays, struct variables in standard C can be assigned as a whole.
Example 1:

#include <stdio.h> 
int main(void)
{ 
    struct student
    {
        long int num;
        int      age;
        char*    name;
    }st1={200,18,"zhangsan"}; 
    struct student  st2,st3;  
    printf("    NO.    age    name   /n");
    printf("   %ld    %d   %s/n",st1.num,st1.age,st1.name);
    getch(); 
    st2.num=199;
    st2.age=19;
    st2.name="lisi";
    printf("   %ld    %d   %s/n",st2.num,st2.age,st2.name);
    getch(); 
    st3=st2;
    printf("   %ld    %d   %s/n",st3.num,st3.age,st3.name);
    getch(); 
    printf("/n/n   struct student:%d",sizeof(struct student));
    getch();
    return 0;
}

Example 2:

#include <stdio.h>
#include <conio.h>
struct birth
    {
        int year;
        int month;
        int day;
    }; 
struct student
    {
        long int num;
        struct  birth  birthday;
        char*    name;
    }st1={200,{1988,8,8},"zhangsan"}; 
int main(void)
{ 
    struct student  st2; 
    st2=st1;
    printf("   %ld     %s  %d/n",st2.num,st2.name,sizeof(int));
    printf("   year: %d   month: %d   month: %d/n",
                st2.birthday.year,
                st2.birthday.month,
                st2.birthday.day);
    getch();
    return 0;
}

Pay attention to
When your struct variable has a member that holds more than one character, it is recommended that you define it as an array (such as the previous name member, which can also be defined as a pointer if you do not know how large the array should be). The reason is that the pointer variable cannot hold the actual data, just the address.


Related articles: