The difference between a struct and a function is that a function is made up of variables of the same data type, and there can be different combinations of data types in the structure.
struct struct Name {
Data type members; //note must be separated by semicolons ;
...
}
To create a construction body
struct Student {
int age;
Char name[45];
char gender;
Float score;
}
//Create a struct variable and assign it a value
struct student Stu ={18, "Luoshuai", ' m ', 87,5};
However: struct student stu1; stu1={18, "Luoshuai", ' m ', 87,5};
Error, if at the time of declaration, not all assigned value, can not be placed in the back to all assignments.
However, you can assign values individually
struct student stu1;
Stu1.age = 18;
Stu1.name= "Lihuahua"; Arrays of strings or arrays cannot be directly assigned to each other using functions.
strcpy (Stu1.name, "Luohuahua");
typedef renamed. Can simplify the function name of the system
typedef float FF;
FF score = 98.5;
typedef and struct combinations used, often used later
typedef struct {
int age;
Char name[30];
char gender;
float weight;
} Cat;
Nesting of structural bodies
typedef struct{
int year;
int mouth;
int day;
} Birthday;
typedef struct{
int age;
Char name[30];
Birthday Bir;
} people;
People p1 = {"Jiesi", 29847901093,{1990,6,12}};
printf ("%d\n", p1.bir.year);
One more thing we need to note is that arrays of strings or arrays cannot be directly assigned to other variables. But the structure is possible between each other. So if you want to swap arrays, you can use structs.
typedef struct {
int age; 4 bytes
Double score; 8 bytes
char gender; 1 bytes
Char name[20]; 20 bytes
} Student;
How do we know where a struct is in the memory footprint? In fact, it is very simple, the above has been calculated for each variable memory bytes Occupy, add up is 33
Our largest member variable data type is the unit, which is obviously a double of 8 bytes, then 8 * n >=, n must go to the value 5. The final memory consumption is 5*8=40 bytes. Often encountered during the interview!
C Language Basics 07