Pointer
&: take the address character and add it in front of a variable, then the combined symbol represents the address of the variable, such as: int a; int *p; p=&a; Then the address value of variable a is assigned to P
A represents the variable a itself, P represents the pointer variable p itself & A represents the address value of variable a * P represents the variable to which the pointer variable p refers
int main(void) { int a = 23; int *p ; //Defines a pointer variable of type int p=&a; //Equivalent to p = (& A); Stored in p is the address of variable a *p = 111; //Equivalent to a = 111; printf("a = %d.\n", a); return 0; }
*: pointer symbol. Pointer symbols have different parsing methods in pointer definition and pointer operation.
int *p; Define the pointer variable P. the meaning of * P here does not represent the variable pointed to by the pointer variable. During definition, the meaning of * here is to tell the compiler that P is a pointer, b=*p prints b as an address, such as P = 0xbfd2c
int p //p is an integer variable
int *p//p is a pointer variable that points to an integer. When using a pointer, * P represents the variable to which the pointer variable p is directed
Pointer definition and initialization
Since the pointer is a variable. Then it can be defined or initialized
First: define first and then assign value
int *p ; // Define pointer variable p
p = &a ; // Assign a value to p
The second method is to initialize while defining
int *p =&a; // The effect is equivalent to the above two sentences
Various types of pointers
Pointer variable is essentially a variable. The type of pointer variable is the common saying pointer type. int *p; Defines a pointer type variable p, and the variable to which this p points is of type int.
#include<stdio.h> int main(void) { float a = 23; int *p = & a; //Error, type mismatch return 0; }
int *p char *p float *p double *p various pointer types must match the type of variable they point to, otherwise the result is unpredictable.
There are two ways to understand pointer definitions:
int *p; First: first, see P, which is the variable name. Second, there is a * in front of P, indicating that the variable p is a pointer variable. Finally, there is an int in front of * P, indicating that the pointer variable p points to an int type data.
Second: first, see p, which is the variable name. Second, see int * in front of p as a whole. Int * is a type (composite type), which represents a pointer to int data
int a[5] = { 555,444 ,333 ,222 ,111}; int *p // p= &a; Compile the alarm, but the execution result is correct. Print 555 //p = &a[0]; Equivalent to P = & (a [0]); The compilation is correct, there is no warning, and the execution is correct. Print 555 //p = a; The compilation is correct, there is no warning, and the execution is correct. Print 555 printf("*p = %d.\n",*p)
Preliminary combination of pointer and array
Array name: when making an R-value, the array name shows the first address of the first element of the array, so it can be directly assigned to the pointer.
If by int a[5]; Then both a and & a[0] represent the first address of the first element of the array, a[0], and & A represents the first address of the array.
Note that the first address of the array is different from the first address of the array. The former is the address of the array element, while the latter is the address of the array as a whole. The two things have different meanings, but the values are the same. From the above, we know that we can use a pointer to point to the first element of the array, so that we can access each element of the array one by one in an indirect way. There are two ways to access arrays.
With int a [5]; int *p ; p = a ;
Array access: a[0] a[1] a[2] a[3] a[4]
Access by pointer: * p * (p+1) * (p+2) *(p+2) * (p+3) * (p+4)
Operation between pointer and + + -- symbol
The pointer itself is a variable, so it can be operated on. However, because the pointer itself stores the address values of other variables, it is meaningless to * /% +. Pointer variables + 1, - 1 are meaningful+ 1 means that the grid pointed by the pointer moves backward one grid, - 1 means that the grid pointed by the pointer moves forward one grid* p + + is equivalent to * (p + +)
What is a structure
A structure is a collection that contains many elements whose data types can be the same. Therefore, structure is a method of data encapsulation. The significance of structure is to encapsulate many variables with different data types to form a large new data type.
#include <stdio.h> /*be careful: 1.The definition of structure type is outside the function, not inside 2.Structure defines a new composite type, not a variable, and does not consume memory. This structure type will be used later to define variables where they are defined. */ struct student { char name[20]; //Student name unsigner int num; //Student number int isMale; //Gender }; int main (void) { struct student s1; // s1 is a variable of type struct student //Assign values to structure variables s1.name[0] = 'j'; si.name[1] = 'i'; s1.name[2] = ','; s1.name[3] = '\0'; s1.num = 123 ; s1.isMale = 1; // Print inspection to see if it is correct printf("s1.name = %s , s1.num = %d ,s1.isMale = %d.\n",s1.name,s1.num,s1isMale) return 0; }
Data structure: organize huge data in a certain way to facilitate operation (search, addition, deletion, etc.), which is called data structure
To work with a structure:
1. Defines the structure type. The definition of structure type is outside the function (outside the function = = global)
2. Use the first defined type to define the structure variable.
3. Use variables. In fact, when you use a structure variable, you use the child elements encapsulated in the structure variable, not the structure variable itself
Initialization of structure
Structural variables are the same as ordinary variables. When they are local variables, if they are defined without initialization or explicit assignment, the value is random
Basics: print double float with% f, char with% c and% d, int with% d, string with% s and pointer with% p
struct mystruct s = {100, 'd', 12.335,111.11111};
perhaps
struct mystruct s = { .a= 1444, .c='k', .f=3.13, .d=32.222222, };
Joint venture
The common body, in terms of definition and use form, is very similar to the structure struct. But the two data structures are completely different things
Structure is the combination and encapsulation of multiple data
Common body, there is only one thing in the common body, only it is named by several names
struct Female { int age; char name[20]; int aerobics; }; union student { struct Male m; struct Female f; }; int main(void) { union student s[30]; // Array that defines 30 students in a class s[0].m.basketball= 47; s[1].f.aerobics = 44 ; }
Macro definition
#include<stdio.h> #define N 321 / / macro definition int main() { int a ; a = 123; a = N ; printf("a= %d ./n", a); return 0 ; }
Enumeration (omitted)