1. Statement on structure
1. Anonymous statement. like:
struct { int i,j; }point;
illustrate:
The meaning of this code is to declare anonymous structure and create a structure variable point. If this declaration is placed in the global domain (outside any function (such as main function), then the variables in point will be initialized to the default value. In other words, when the structure variable is declared in this way, memory space has been allocated for it.
Only one variable is needed to be generated for this structure! In this example, the anonymous structure will have and only have the structure variable point!
Different anonymous structure variables have different types! like
struct { int i,j; }p1,p2; struct { int i,j; }p3;
If p1=p2 is set, then ok; if p1=p3 is set, the compiler prompts "incompatible types when assigning to type 'struct <anonymous>' from type 'struct <anonymous>'", and the actual types of the two are different.
2. Explicitly declare a structure
struct node{ int i,j; };
Declare a struct node. If you need to declare one of its objects, you can do this: struct node n1;
Multiple variables of this structure can be declared.
Differences between "struct variables in C" and "class objects in Java". In C, "struct node n1;" creates a structure variable and allocates memory space to it, not necessarily initialized! It depends on whether the variable is in the global domain; in Java, "Node n1;" just declares a class object, that is, a "null reference", which can be imagined as a null pointer in C. When "n1 = new Node();", n1 points to the memory space of the object. Therefore, in Java, you can judge whether the object is empty by "n1==null"; in C, you cannot judge by "n1==NULL", because "n1" is not a pointer, but the name of a type variable, just like "int a;", obviously "a" is not a pointer!
3. Use typedef to simplify the writing of structures
typedefstruct { int i,j; }Node;
It's equivalent to rename the code to Node. In the past, I needed to declare "struct node n1;" like this, but now I only need "Node n1;".
In this code, if there is no typedef, the code means "declare an anonymous structure variable"! Pay attention to the difference.
4. Declare structure variables in structure.
typedef struct { int i,j; Node n1; }Node;
This code is wrong!
Error 1: If you declare another structure directly in the structure, there will be a dead loop, such as A includes B, B includes A, A includes B... so that the compiler cannot know the space size of the structure, so it cannot be compiled!
Error 2: typedef has not named the structure Node yet, you have used Node in the structure. Obviously, the compiler still doesn’t know what Node is! So, it cannot be compiled!
The correct way to use it is as follows:
typedef struct node{ int i,j; struct node *n1; }Node;
2. Assignment of structure
1. Declare the default value after a variable
typedef struct { char *p; int i; char ch[256]; }mystr; mystr str;//Declare a variable, and the space has been allocated for it at this time!
As mentioned earlier, if this variable declaration is global, then "equal to NULL, equal to 0, the array is '\0'", which is the default initial value; if not global, all values are "wild values".
2. Manual initialization
mystr str2={"abc",2,"def"}; mystr str3={.p="abc",.ch="def"}; mystr str4={.ch[256]="def"};//error! mystr str5={.ch[10]="def"};//right!
At this time, the initial value is manually assigned when str2 is declared. The behavior is different from the assignment! It is a character pointer, that is, pointing p to the address of the constant string "abc" in memory; it is a constant character pointer (the pointer cannot be operated), which represents a character array, that is, copying the constant string "def" character character into the ch array. After the assignment is completed, the value of ch is: 'd','e','f','\0','\0'...
It is also possible to initialize certain variables in the structure like str3, which is worth noting that str4 and str5. For arrays (such as char a[size]), passing to constant character pointers can be "a", "a[n]" (0<=n<size, the compiler will ignore n), and cannot be "a[size]" (the compiler will detect and report "array index in initializer exceeds array bounds").
3. Assignment
mystr str6; = "abc";
or
mystr * pstr = & str6;//Get the pointer to this structure variablepstr->p = "abc";
4. Dynamically generate structure variables
mystr * pstr = (mystr*)malloc(sizeof(mystr)); pstr->p = "abc";
Note that if it is a dynamically generated structure variable (using malloc), it must be freed before discarding the variable (using free).
If there are also dynamically generated objects inside the structure, the internal memory space must be released before releasing the structure, as follows
pstr->p = (char*)malloc(sizeof(char)*256); free(pstr->p); free(pstr);
3. Structure array
We know that if the variable array of basic data types is directly defined, the space can be allocated. Structures can be regarded as a new type. They also automatically allocate space after defining variables. The same is true for the array of structures.
struct book library[10];
This defines an array with 10 book variables and has allocated storage space. The indexing method of structures is the same as that of ordinary arrays.
Structural arrays can also be initialized using literal values, as follows
struct book lib[2] = { {"apue", "stevens", 128.0}, {"cpp", "prata", 60.0} };
Isn't it very convenient? Note that the outermost part is { , not [ oh. For structure variables whose members are structures, you can also use literal initialization. Remember, it is only initialization and cannot be used to assign values to structure variables.
4. Pointer to the structure
The pointer to the structure is a point that has never been well-known. I hope it can be recorded here to enhance understanding.
There are several benefits for pointers. First: Just as pointers to arrays are easier to operate than the array itself, pointers to structures are usually easier to operate; Second: In early C, only pointers to structures could be used; Third: Many wonderful data representations use structures containing pointers to other structures.
Unlike arrays, the name of a structure is not the address of the structure (that is, the individual structure name is not synonymous with the address of the structure), and the & operator must be used. The way of declaring a pointer is no different from a normal variable:
struct book *cpp; struct book c = { "c primer plus", "prata", 60.1 }; cpp = &c;
Assuming that lib is an array of struct book, now use the structure pointer cpp to point to lib[0], then according to the pointer's operation rules, cpp+1 will point to lib[1]. Although in general, the elements in the structure are arranged at one time in memory, the address difference between cpp+1 and cpp can be calculated based on the size of each element. However, considering the system's memory alignment requirements, different systems may be aligned differently, so it is inappropriate to calculate the storage size of the structure by adding up the size of each member.
V. Members of the access structure
This is relatively simple. Note that the structure and pointers to the mechanism access members differently. The structure itself uses the . operator to access, while the pointers to the structure use -> access.
strcut book cpp, *pcpp; ... char *title; title = ; // title = pcpp->title; // title = (*pcpp).title; // Because the priority of . is higher than *, it must be bracketed