iOS Training--My C language notes, look forward to communicating with you!
One: Introduction to structs and enumerations:
Structs and enumerations: is a kind of storage complex data
struct: is a user-defined type, a collection of different types, and an array is a collection of variables of the same type.
II: Structure creation struct user{char username[20]; int score;} Here are two different types of properties of the struct body. Creating a struct is creating a type, and the computer does not allocate memory space. Third: struct call struct user U1, this is the call to struct, this time the computer will allocate memory space for it. U1.USERNAME[20] = "Tom"; u1.score = 98; The above two sentences are the assignment of the structure. Four. Pointer to struct char * p = &u1; Description p is the address of the variable U1 that refers to the user type; U1.score = 98 can also be written (*p). score = 98; introduce a new concept: P->score, which represents(*p). score; Let's take a look at an exercise#include struct test{int A; int b; char c;}; int main (void) {struct Test S1 = {9,2, ' A '}; struct Test * p = &s1; printf ("%d%d%c\n", s1.a, s1.b, s1.c); P->a = 5; P->b = 4; P->c = ' C '; printf ("%d%d%c\n", s1.a, s1.b, s1.c); return 0;} V: Introduction to Enumerations
Enumeration: not suitable for normal types or structs so you can use enumeration VI: The definition of an enumeration #define MON 0
#define TUE 1
#define WED 2
#define THR 3
#define FRI 4
#define SAT 5
#define SUN 6
printf ("Mon =%d\n", Mon);
printf ("TUE =%d\n", TUE);
printf ("WED =%d\n", WED);
printf ("THR =%d\n", THR);
printf ("FRI =%d\n", FRI);
printf ("Sat =%d\n", Sat);
printf ("Sun =%d\n", Sun);
} VII: Use of enumerations we combine the above definitions to enumerate the user qualified enum main () {
Enum Weekday {Sun,mon,tue,wed,thu,fri,sat} a,b,c;
A=sun;
B=mon;
C=tue;
printf ("%d,%d,%d", a,b,c);
} Enumeration Frankly: C helps you to encapsulate a define collection, with easy-to-remember characters representing constant eight: enumeration and struct Summary enumeration provides a convenient way to use groups of related constants and to associate constant values with names. For example, you can declare an enumeration for a set of integer constants that correspond to seven days of the week, and then use the seven-day names in your code instead of their integer values.
There are the following additional restrictions for enumerations:
They cannot define their own methods.
They cannot implement interfaces.
They cannot define properties or events.
Structs are used to construct data structures that contain data and functions.
Enumerations are two different things from structures, nothing comparable. Enumerations are values, structures are data structures, in other words, structs are collections of values and methods. Structure and enumeration types from the point of view of program implementation, the data is expressed in a way that is closer to natural language. For example, to implement a 2-dimensional point, you can use a 2-dimensional array, but the readability will be poor. At this point, you can swap the structure with attribute items x, y, which is closer to the way we actually use them.
An enumeration type is essentially a collection of tags, with enumerated variables for readability. For example, there are five states of a process: Create, prepare, run, stop, destroy, and if you use numbers to represent the state, we use 0~4 to represent the line, but others will not understand that we declare EUNM Status{init,ready,run,stop by enumeration type, Destroy}, you can make your program more readable
"Good Programmer's note sharing" structure and enumeration of--c language