Basic operations of struct, basic operations of struct
1 # define _ CRT_SECURE_NO_WARNINGS 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 6 // define a struct 7 // define a data type. Alias with fixed memory size, no memory allocated 8/* struct Teacher 9 {10 char name [5]; 11 int age; 12 }; */13 typedef struct Teacher14 {15 char name [64]; 16 int age; 17 int id; 18} Teacher; 19 20 21 struct Student22 {23 char name [64]; 24 int age; 25} s1, s2; // define the type and define the variable 26 27 struct28 {29 char name [64]; 30 int age; 31 32} s3, s4; // anonymous type definition variable 33 34 // three methods for initializing the variable 35 // define the variable and then initialize 36 // 37 Teacher t7 = {"aaaaa", 18, 01 }; // global 38 struct Student239 {40 char name [64]; 41 int age; 42} s5 = {"names", 21 }; 43 44 struct45 {46 char name [64]; 47 int age; 48 49} s6 = {"names", 30}; 50 int main () 51 {52 // struct Teacher t1; // tell the C compiler to allocate the memory to me 53 Teacher t1; 54 Teacher t2 = {"aaaaa", 18, 01 }; // define the variable and initialize 55 56 t1.age = 32; // t1. is the 57 // offset of the addressing operation to calculate the t1 variable relative to the t1 variable ===, and is calculated in the cpu, no memory operated 58 59 // the memory space operated by pointer 60 {61 Teacher * p = NULL; 62 p = & t2; 63 printf ("p-> age: % d \ n ", p-> age); //> is the 64 // offset = Of The addressing operation relative to the t2 variable, which is calculated on the cpu, no operation memory 65 printf ("p-> name: % s \ n", p-> name); 66} 67 strcpy (t1.name, "James "); 68 printf ("ti. name % s \ n ", t1.name); 69 system (" pause "); 70 return 0; 71}