Some key points in the C language written by the struct struct

  • 2020-05-10 18:30:33
  • OfStack

1. Statements about structures
1. Anonymous statement. Such as:


struct {
  int i,j;
}point;

Description:
This code declares a struct with no name (anonymous) and creates a struct variable point. If this declaration is placed in the global domain (outside any function, such as the main function), then the variables within point will be initialized to default values, in other words, memory space is allocated for the struct variable when it is declared in this way.
Only 1 variable is needed to apply to this structure! In this case, the anonymous struct will have and only have the struct variable point!
Different types of anonymous structure variables are different! Such as


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

If p1=p2, ok; If p1=p3, the compiler prompts "incompatible types when assigning to type struct 'struct" < anonymous > 'from type' struct < anonymous > '", the actual type of the two is not the same.

2. Explicitly declare a structure


struct node{
  int i,j;
};

A struct struct node is declared. If you need to declare an object for it, you can do this: struct node n1;
You can declare more than one variable for this structure.
Distinguish between "struct variables in C" and "class objects in Java". In C, "struct node n1;" Created a structure variable and allocated memory space for it, not 1 definite initialization! It depends on whether the variable is local; And in Java,"Node n1;" It just declares a class object, that is, a "null reference ", which can be thought of as a null pointer in C, when "n1 = new Node();" , n1 points to the object's memory space. Therefore, in Java, we can judge whether the object is empty by "n1==null"; In C, you cannot say "n1==NULL" because "n1" is not a pointer, but the name of a type variable, like "int a;" Well, obviously "a" is not a pointer!

3. Simplify the structure with typedef


typedefstruct {
  int i,j;
}Node;

It's like changing the code to Node. It used to be required to declare "struct node n1;" Now you just need "Node n1;" .
In this code, if there is no typedef, the code means "declared an anonymous struct variable"! Notice the difference.
4. Declare the struct variables in the struct.


typedef struct {
  int i,j;
  Node n1;
}Node;

This code is wrong!
Error 1: directly declare another structure in the structure, and there will be a dead loop, such as A including B, B including A, A including B... Makes the compiler unable to know the size of the structure space, therefore, can not be compiled!
Error 2: you are using Node in a structure before typedef has named it Node. Obviously, the compiler does not know what Node is yet! Therefore, it cannot be compiled!
The correct usage is as follows:


typedef struct node{
  int i,j;
  struct node *n1;
}Node;

2. Assignment of the structure
1. The default value after declaring a variable


typedef struct {
  char *p;
  int i;
  char ch[256];
}mystr;
mystr str;// The statement 1 A variable , Space has been allocated for it at this point! 

As mentioned earlier, if the variable is declared globally, "str.p = NULL, str.i = 0, str.ch array is '\0'", which is the default initial value. All values are "wild" if they are not global.

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 point, the initial value is manually assigned when str2 is declared. str2.p and str2.ch do not behave the same! str2.p is a one-character pointer, which refers to the address of the constant string "abc" in memory. str2.ch is a pointer to a constant character (unable to manipulate a pointer), which represents an array of characters. In other words, the constant string "def" is put into the ch array character by character copy. After the assignment, the value of ch is: 'd','e','f','\0','\0'...
You can also initialize some of the variables in the structure, such as str3, notably str4 and str5. For arrays (such as char a[size]), passed to the constant character pointer, it could be "a", it could be "a[n]"(0 < =n < size, the compiler will ignore n), not "a[size]"(the compiler will detect "array index in initializer exceeds array bounds").

3, the assignment


mystr str6;
str6.p = "abc";

or


mystr * pstr = & str6;// Get a pointer to the struct variable 
pstr->p = "abc";

4. Generate structural variables dynamically


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

0

Note that if it is a dynamically generated struct variable (malloc is used), its memory space must be freed before the variable is discarded (free).
If there are also dynamically generated objects inside the structure, the internal memory space should be freed before the structure is released, as shown below


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

1

3. Struct array

We know that the array of variables of the basic data type is directly defined to allocate space, and the struct can be viewed as a new type, which is automatically allocated space after the variables are defined and declared, and the array of the structure is the same.


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

2

This defines an array with 10 book variables and has allocated storage space. The array of the struct is indexed in the same way as the normal array.

Struct arrays can also be initialized using literal methods, as shown below


struct book lib[2] = {
 {"apue", "stevens", 128.0},
 {"cpp", "prata", 60.0}
};

Isn't that convenient? Note that the outmost is {, not [oh. For struct variables whose members are 1 structure, you can also initialize with literals, remember, just initialization, not assignment to struct variables.

4. Pointer to structure

The pointer pointing to the structure is 1 point and 1 point is not well grasped. I hope you can record 1 point here to enhance your understanding.

Pointers to structures are generally easier to manipulate, just as Pointers to arrays are easier to manipulate than arrays themselves. 2. In the early C, parameter passing could only use Pointers to structures; 3: many fancy data representations use structures that contain Pointers to other structures.

Unlike arrays, the name of a structure is not the address of the structure (that is, a separate structure name is not a synonym for the address of the structure) and the ampersand operator must be used. Declaring a pointer is no different than declaring a normal variable:


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

4

If lib is an array of struct book, and cpp is now used to point to lib[0], then cpp+1 will point to lib[1] according to the operation rules of the pointer. Although in the 1-like cognition, the elements in the structure are arranged once in memory, so the address difference between cpp+1 and cpp can be calculated according to the size of each element. However, considering the system's requirements for memory alignment, different systems may not align in the same way, so it is not appropriate to calculate the storage size of the structure by adding the size of each member.

5. Access members of the structure

This one is relatively simple. Notice that the structure and the pointer to the mechanism access members in different ways. The structure itself is accessed using the. Operator, while the pointer to the structure USES - > Access.


struct {
  int i,j;
}p1,p2;

struct {
  int i,j;
}p3;

5


Related articles: