C structure Summary
Struct declaration:
# Include <stdio. h> // The first method is struct student {int age; float score; char sex ;}; // The second method is struct student2 {int age; float score; char sex ;} st2; // method 3 struct {int age; float score; char sex;} st3; int main (void) {struct student ST = {80, 66.6, 'F'}; return 0 ;}
Assign values and initialize struct variables:
1.
# Include <stdio. h> // method 1 struct student {int age; float score; char sex ;}; int main (void) {struct student ST = {80, 66.6, 'F'}; // assign the initial value struct student st2 while initializing the definition; st2.age = 10; st2.score = 88; st2.sex = 'F '; printf ("% d % F % C \ n", St. age, St. score, St. sex); printf ("% d % F % C \ n", st2.age, st2.score, st2.sex); Return 0 ;}
2.
# Include <stdio. h> // method 1 struct student {int age; float score; char sex ;}; int main (void) {struct student ST = {80, 66.6f, 'F'}; // assign the initial value struct student * PST = & St; // & St cannot be changed to St PSt-> age = 88; // method 2 St. score = 66.6f; // The first method 66.6 is of the double type by default in the C language. If you want a real number to be of the float type, you must add f or f at the end, so 66.6 is of the double type, 66.6f or 66.6f is float printf ("% d % F \ n", St. age, PSt-> score); Return 0 ;}
Reference: http://www.cnblogs.com/ligongzi/archive/2012/08/24/2654448.html
Struct student {char name [20]; char sex; int age; char ADDR [40] ;};/* then defines a student variable of the student type */struct student;
Maybe I was influenced by Java, and I once wrote it like this:
struct man{ int age = 30; int score = 80;};int main(){ man man1 = {20,70};}
The result cannot be compiled. Because this is a struct that I created with the default value and has never been seen by the compiler.
Struct member variables cannot be assigned values in the Declaration.
The correct statement is as follows:
Struct man {int age; // This completes int score;}; int main () {struct man Man1 = {30, 80 };}
It is obviously cumbersome to write struct every time you define a struct,
The refined C language is used for typedef to facilitate definition and use:
typedef struct Man{ int age; int score;}man;int main(){ man man1 = {20,80}; man man2 = {30,70}; man man3 = {40,99}; printf("%d\n",man1.age); printf("%d\n",man3.score);}
In this way, there is no problem with how many "men" you want to summon. Another extreme problem is whether the declared struct name and the defined struct variable name can be the same? Let's try:
Typedef struct man {int age; int score;} Man; // also called man. Do you have any comments? Int main () {MAN man = {40, 50}; // also called man. Is there a problem? Printf ("% d \ t % d \ n", man. Age, man. Score );}
Compilation and running are all correct. Try it yourself.
Then let's discuss the points:
Struct student {char name [20]; char sex; int age; char ADDR [40] ;};/* then defines a student variable of the student type */struct student;
Assign values for name and age in the defined structure. Use the following statement:
Strcpy (student-> name, "Jack"); Student-> age = 21;Student-> Name is the abbreviation of (* student). Name.
It should be pointed out that the structure pointer is a pointer to the structure and is the first address of the first Member in the structure. Therefore, we should initialize the structure pointer before use, that is, to allocate the byte space of the entire structure, which can be completed using the following function. The preceding example is described as follows:
student=(struct string*)malloc(size of (struct string));
Size of (struct string) automatically calculates the byte length of the string structure. The malloc () function defines a memory area with the size of the structure length, and then returns the fraudulent address as the structure pointer.
Note:
The structure variable name does not point to the address of the structure, which has different meanings from the array name. Therefore, the first address of the first Member in the structure should be & [structure variable name].
Demo.
/* Example at 9:17:43 on the first day of the month: Sending address or sending content purpose: One of the advantages of pointer: fast data transmission, low memory consumption, fast execution */# include <stdio. h> # include <string. h> struct student {int age; char sex; char name [100] ;}; // The semicolon cannot save void inputstudent (struct student *); void outputstudent (struct student *); int main (void) {struct student st; // 15 rows // printf ("% d \ n", sizeof (ST); inputstudent (& St ); // The input of the struct variable must send the st address outputstudent (& St ); // The output of the struct variable can either send the address of the St or directly send the content of the St, but to reduce memory consumption and improve the execution speed, the recommended sending Address is return 0 ;} void outputstudent (struct student * PST) {printf ("% d % C % s \ n", PSt-> Age, PSt-> sex, PSt-> name );} void inputstudent (struct student * pstu) // pstu occupies only 4 bytes {(* pstu ). age = 10; strcpy (pstu-> name, "Zhang San"); pstu-> sex = 'F ';} /* // This function cannot modify the st value of 15 rows of the main function. Therefore, this function is incorrect void inputstudent (struct student Stu) {Stu. age = 10; strcpy (Stu. name, "James"); // cannot be written as Stu. name = "James"; Stu. sex = 'F ';}*/