* Meaning
int * p;// defines a variable named P that can hold an int data type address 3. Pointer operator, // If p is a pointer variable that is already defined, then *p represents a variable with the content of P as the address
Print data type
%d - intlongint%c char%f- float Double int%o- octal output %s– string
3. Pointers
The pointer is the address. Warm up applet introduction pointers int // P is the name of the variable, int * indicates that the data type of the P variable is the data type of the address that holds the int type // int * p; does not mean that a variable named *p is defined // int * p; This should be understood: p is the variable name, int * is the data type, and the data type of the P variable is int* // the so-called int * type is actually the type that holds the INT variable address int i = 3= &i; /* 1. P holds the address of I, so p points to I 2. P is not i,i nor p, the value of modify p does not affect the value of I, and the value of I will not affect the value of P 3. If a pointer variable points to a normal variable, the * pointer variable is exactly the same as General Variable example: if P is a pointer variable and p holds the address of the normal variable I, p points to the normal variable i
*p is exactly the same as I in all occurrences of the *p can be replaced by I in all occurrences of I can be replaced by *p *p is the content of the p as the address of the variable * * = *p; // equivalent to j = i; printf ("I =%d, j =%d\n", I, j);
4. Pointers and Arrays
array name, subscript and pointer relationship, pointer variable operand array name int // A is the array name, 5 is the size of the array, the number of elements int // 3 Rows 4 columns A[0][0] is the first element of the array Int b[5]a=b; // Error a one-dimensional array name is a pointer constant that holds the address of the first element of a one-dimensional array int a[5]; int a[3][4];p rintf ("% #X \ n", &a[0]);p rintf ("% #X \ n",& * (P+i)
5. Dynamically allocating memory
dynamic memory allocation problems The disadvantage of traditional arrays 1. The array length must be specified and can only be a constant integer. int a[5]; int Len; int A[len]; // Error 2. Traditional form of an array, the programmer cannot manually free the space array once defined, the space allocated by the system for the array has been run and the space of the array is freed 3. The length of the array cannot be dynamically increased in the function run, or the 4.A function definition of the array is used only before the completion of a, the function of a can not be used by other functions
# include <stdio.h> # include <malloc.h> // cannot save malloc is memory (RAM) Allo Cate (Assignment) abbreviation int main ( void ) { int i = 5; // allocated 4 bytes statically allocated 11 rows int * p = ( int *) malloc (4); // 12 Lines /* 1. To use the malloc function, you must add the malloc.h header file 2. The malloc function has only one parameter, and the shape The parameter is integral type 3. 4 indicates that the request system allocates 4 byte 4 for this program. The malloc function can only return address 5 of the first byte. 12 rows are allocated 8 bytes, the p variable is 4 bytes, and p points to 4 bytes of memory 6. The memory occupied by P itself is statically allocated, and P points to the memory that is dynamically allocated */ *p = 5; // The *p represents an int variable, except that *p the integer variable's memory allocation method and the 11-row I variable are allocated differently free (p); // The Freep (p) indicates that the memory that P is pointing to releases p itself is static and cannot be released manually by the programmer, and the memory of P itself can only be automatically released by the system at the end of the function where the P variable is running printf ("Hello everyone!" \ n "); return 0 ;}
6. Function pointers
1. Define INT (*PF) (int x, int y); 2. Assignment PF = add; 3. Reference PF (3,5);