Learning articles: structure

In the C language , a structure (struct) refers to a data structure, is the aggregate data type in C language (aggregate data type) of a class. Structure may be declared as variables , pointers , or array , etc., to achieve more complex data structures . But also some elements of the structure of the collection , these elements are called members of the structure (member), and these members can be of different types, members of the general access by name.

There are several forms of the structure:

If every person has a name Height gender-specific, we use the structure can be expressed as

struct person{
    char name[50];
    float height;
    int sex;
};
struct person man;
struct person women;
or
struct person{
    char name[50];
    float height;
    int sex;
}man,women;
Implemented and used in the project are as follows
#include <stdio.h>
#include <string.h>

struct person{
    char name[50];
    float height;
    int sex;
}man,women;
void printPerson(person per);

int main () {
    strcpy (man.name, " John Doe " );
    man.height = 150;
    man.sex = 1;
        
    strcpy (women.name, " John Doe " );
    women.height = 170;
    women.sex = 2;
        
    printing person (man);
    printf("\n"); 
    printPerson(women);
    return 0;
}

void printPerson(person per){
    printf("name : %s\n",per.name);
    printf("height : %.1f\n",per.height);
    printf("sex : %d\n",per.sex);

}

Print results are as follows

 

Reproduced in: https: //www.cnblogs.com/lovemargin/p/10568422.html

Guess you like

Origin blog.csdn.net/weixin_33712987/article/details/93471039