当前位置:网站首页>Application and examples of C language structure

Application and examples of C language structure

2022-06-13 01:57:00 richardgann

1. Structure concept  
2. Structure declaration  
3. Structure definition  
5. Reference of structure variable member  
6. Assignment of structure variables  
7. Initialization of structure variables  

8. Nesting of structures

1. Structure concept

C The language introduces a kind of structure to construct the sentence type , He is made up of several members , The member itself can be a basic data type , It can also be other construction types , He is quite similar to the record in high-level languages , Similar to java Entity classes in high-level languages . Can represent some complex data types ; similar :

struct student{
    char *name;
    int age;
    int num;
    float weight;
    };

Declaration of a structure :

Structure before use , You need to declare the structure first  

Declaration syntax :

struct  Structure name 
{
     Type description symbol   Member name ;// Member list 
};

A member table consists of several members , Each member is part of the structure , Each member must have a type description , Members can be basic data types , You can also construct data types ;

  1. {} Followed by a semicolon ”;” Of
  2. The structure can be declared in the beginning of the source file program , It can also be declared in the header file , Usually in the actual development process , Are defined in the header file ;
  3. The name of a structure and its members can be the same , Structure members and other variables can also have the same name ,
  4. The name of the structure type must be ”struct Structure name ”, Be careful struct Keywords cannot be omitted ;
  5. The structure needs to be declared to be in use
  6. The use of the structure must Define the corresponding structure variables
  7. The structure type is equivalent to a model , No memory is allocated when declaring , Only when defining the corresponding structure variable , To allocate the actual memory

The definition of structural variables :

grammar 1: The structure has been declared in advance

 1.   struct  Structure name   Variable 1, Variable 2,... Variable n;
grammar 2: When you declare a structure, you also define its variables
 1. struct   Structure name {
 2.       Member list ;
 3.   }  Variable name 1, Variable name 2... Variable n;
grammar 3: Define structure variables directly
1.  struct {
2.        Member list ;
3.    } Variable name 1,..., Variable name n;

Be careful : The definition of structure variables should not be in the header file , When declaring the structure, put it in the header file , Structures and structural variables are similar to java The relationship between classes and objects in .

Reference of structure variable member

C Language does not allow the input and output of structural variables as a whole , All operations on structural variables are operations on structural variable members . It's an individual relationship , Cannot operate as a whole  

How to quote :

 Structural variable . Member name 
 The body variable members of the mechanism are passed "." To quote 
 If the member itself belongs to a structure type , At this point, we will continue to use a number of points to calculate symbols , Level by level , Until you find the one you operate 
class.student.name = "zhangsan";
 We can refer to the address of the structure variable member , You can also refer to the address of a structure variable  

Assignment of structure variables

Initialization method 1: 
Assignment of structure variables , It is to assign values to each member of the structure variable  
Such as :student.xh = 100;

 student.name = "hello";

You can assign values with input statements :

    scanf("%s%f",&student.sex,&student.score);
Structure variables of the same type are allowed to be assigned to each other :
 student1 = student2;
Be careful : If the member variable in the structure is a character array , Cannot assign , But you can use strcpy Copy , If it is a character pointer , You can assign values directly , So we try to use character pointer to operate in the process of operation ;

Be careful : The array name is the address , So when assigning values , No need to add & Symbol :

char gender[30];
 Such as :scanf("%s",gender);
When there is no assignment initialization , Since structural variables are local variables , So it's stored on the stack , Its initial value is a random number ; The number of bytes occupied is equal to the total number of bytes occupied by its member variables , The more members , The more bytes it takes

Initialization method 2: 

The structure is declared in advance, then the structure variable is defined and initialized :

struct  Structure name   Variable name 1 = { Member value class table },...,
                Variable name n = { Member value list };
Initialization method 3: 
The structure variable is defined and initialized when the structure is declared :
struct  Structure name {
         Member variables ;
    } Variable name 1 = { List of member values }, Variable name 2 = { List of member values },..., Variable name n ={ Member value list };
Initialization mode 4: 

Directly define structure variables and initialize

strcut {
     Member list ;
} Variable name 1={ Member value list },..., Variable name n={ Member value list };

Nesting of structures

You can put one structure into another , But a structure cannot nest itself , If you nest yourself , Must be a struct pointer type

struct address{
    char country[20];
    char city[20];
}
struct student{
    int xh;
    char name[20];
    struct address addr;
};
 To access the structure address Members of the , and address It's another structure student Members of , It can be accessed through the chain of dot operators 
 Such as : Visiting members country: stu.addr.country
    visit city:stu.addr.city;

Here is a complete display of the structure playing method through the code :

#ifndef __TEACHER_H_
#define __TEACHER_H_
/* Define a street structure , The member variable is country , City , And streets */
struct address{
    char *country;
    char *city;
    char *street;
};

/* Define a teacher Structure , Member variables are numbered , Age , full name , Working years , Address */
struct teacher{
    int num;
    int age;
    char *name;
    int teach_year;
    struct address addr;
};
#endif

Source code

#include<stdio.h>
#include<stdlib.h>
#include"teacher.h"
#include<string.h>

struct score{
    int math_score;
    int chinese_score;
    int english_score;
};

/* Define a student type */
struct student{
    int xh;//
    char *name;
    int class;
    struct address addr;
    struct score all_score;
}zzf ={1,"zzf",3,{"china","shanghai","yindu_road"},{80,85,90}};// Define a student directly when declaring zzf, And initialize .

// Print out student information 
void printfStudentInfo(struct student stu);

void printfTeacherInfo(struct teacher tea);

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

    printfStudentInfo(zzf);
    printf("==========================\n");

    // Initialization when defining 
    struct teacher teacher_zhang = {1,50,"zhang",5,{"china","minhang","yizhong"}};
    printfTeacherInfo(teacher_zhang);

    printf("===============================\n");
    // Define first and then initialize 
    struct teacher teacher_wang;
    teacher_wang.num = 2;
    teacher_wang.age = 40; 
    teacher_wang.name = "wang";
    teacher_wang.teach_year = 10; 
    teacher_wang.addr.country = "china";
    teacher_wang.addr.city = "shanghai";
    teacher_wang.addr.street ="chunshen_road";
    printfTeacherInfo(teacher_wang);

    printf("============= Same structure name variable jiegouti assignment ======\n");
    struct teacher teacher_other;
    teacher_other = teacher_wang;
    printfTeacherInfo(teacher_wang);

    printf("==============================");
    // Do not define the structure name , Then directly define the structure variables 
    struct{
        char book_name[20];
        int pages;
        int charpter_nums;
    }math_book;
    // If you use character arrays , Must use strcpy Can be assigned 
    strcpy(math_book.book_name,"jihe");
    math_book.pages = 150;
    math_book.charpter_nums = 12; 

    printf("book name:%s,book pages:;%d,book charpter_nums:%d\n",math_book.book_name,math_book.pages,math_book.charpter_nums);

    return 0;
}

void printfStudentInfo(struct student stu){
    printf("stu xh:%d\n",stu.xh);
    printf("stu name:%s\n",stu.name);
    printf("stu class:%d\n",stu.class);
    printf("student country:%s,city:%s,street:%s\n",stu.addr.country,stu.addr.city,stu.addr.street);
    printf("student math:%d,chinese:%d,english:%d\n",stu.all_score.math_score,stu.all_score.chinese_score,stu.all_score.english_score);
}

void printfTeacherInfo(struct teacher tea){
    printf("teacher num:%d\n",tea.num);
    printf("teacher age:%d\n",tea.age);
    printf("teacher name:%s\n",tea.name);
    printf("teacher teacher_year:%d\n",tea.teach_year);
    printf("teacher country:%s,city:%s,street:%s\n",tea.addr.country,tea.addr.city,tea.addr.street);
}









原网站

版权声明
本文为[richardgann]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280548347136.html