The first few sections of the explanation, mainly memory address and pointer analysis. This section explains the easy confusing keywordconstant and the knowledge of the structural body.
First, Constkeyword
1. Pointer to character constants
char const *P1 = "Hello";p rintf (the value of the pointer:%p, the value pointed to by the pointer:%c\n ", p1, *p1);p 1++;p rintf (" Value of the pointer:%p,\n ", p1);
In the example above. P1 is a pointer to a character constant, its characteristics: the value of the pointer can be changed. The value pointed to by the pointer cannot be changed.
So. p1++ This operation is possible, printing results such as the following:
The value of the pointer: 0x100000f22, the value pointed to by the pointer: H
Value of the pointer: 0x100000f23
Let's try to write (*P1) + +; This operation. First, the *P1 operation obtains the h in the string "Hello" and then the "+ +" operation, but at this point, "Hello" is read-only, so this code will error during compilation.
2. Constant pointers to characters
char *const p2 = "World";p rintf ("The value of the pointer:%p, the value pointed to by the pointer:%c", p2, *P2);
in the example above, p1 is a constant pointer to a character. Its characteristics: The value of the pointer can not be changed, the value pointed to the pointer can be changed.
Printing results:
The value of the pointer: 0X100000F64, the value pointed to by the pointer: w
We'll try to write the p2++ again; This operation. This will cause an error during compilation, because P2 is read-only.
*P2 = ' k '; This operation does not error during compilation, so it does not violate the "pointer-to-value can be changed" rule, but it will error during execution, because the string "world" is stored in a read-only store.
Second, the structural body
In C, a struct is used to store values of different types together, so a struct is a collection of values called its members.
In the array, you can access it by subscript. Just because the element length of the array is the same.
The structure is not able to, so in the structure, each member has its own name, they are interviewed by the name.
Statement of the structure of the body
Way one: Basic statement
struct { int A; Char b; float C;} x;struct { int A; Char b; float C;} Y[20], *z;
the first struct declaration creates a variable named X. It consists of three members: an integer, a character, and a floating-point number.
The second struct declaration creates an array of Y and Z,y, which includes 20 structures. Z is a pointer to the structure of this type.
But. These two declarations are treated as two distinct types by the compiler. Even if their members list is exactly the same, because z = &x; is the wrong wording.
Way two: Label statement
struct Simple { int A; Char b; float C;};
The label agrees that multiple declarations use the same member list.
This statement links the label simple to this member list. The declaration does not provide a list of variables, so it is created regardless of the variable.
struct simple x;struct simple y[20], *z;z = &x;
and at this point z = &x; is fully capable. Because X and z point to a structure that is the same type of structure.
Way three: typedef declaration
typedef struct { int A; Char b; float C;} Simple;
This technique is almost the same as declaring a structure tag effect. The difference is that simple is now a type name rather than a structure tag.
So perhaps the statement might look like this:
Simple x; Simple y[20], *z;
Use of the structure body
Definitions such as the following structure body
typedef struct { int A; Short b[2];} ex2;typedef struct EX { int A; Char b[3]; EX2 C; struct Ex *d;} Ex;
There is an shaping member and a short array in the structure Ex2, and there is an shaping member in the struct ex. An array of characters, a EX2 structure, a pointer variable pointing to the ex structure.
The following, initialize them to work
Ex x = {ten, "Hi", {5, {-1, 25}}, 0}; Ex *px = &x;
Analyze the results:
printf ("PX Storage content:%p\n", px);
The content stored in PX is the address of structure X.
Ex accept = *px;
*px is to get the value of the entire structure that the PX points to.
printf ("The value of variable a of structure x:%d,%d\n", X.A, Px->a);
There are two ways to access struct members:
1. struct. Variable name
2. Variable name, pointer name
printf ("The address of variable a of structure x:%p", &px->a);
Gets the address of the member variable in the structure (note: The address printed here is the same as the PX address.) But the meaning is different: px represents the address of the entire structure, and &px->a represents the address of the variable a in structure X. Because variable A is the first element of the structure. So the address of the entire structure is consistent).
C language: Constkeyword, structural body