Pointer OverviewAll variables are allocated with one piece of memory in the memory space. The pointer is used to represent the address of the memory space. You can use the address operator & to obtain the address of a variable in the memory space, if you want to store an address, you can use a pointer variable to store the memory address. The pointer variable is used to represent a memory address, and the pointer variable is used to store the memory address.
# Include <stdio. h> # include <stdlib. h> main () {int I = 10; int * P = & I; printf ("% # x \ n", P); // P = NULL; // printf ("I = % d \ n", I); // I = 10 // printf ("* P = % d \ n", * P ); // * P = 10 // * P = 100; // printf ("I = % d \ n", I); // I = 100 I = 90; printf ("* P = % d \ n", * P); // * P = 90/** summary p is a pointer variable, the memory address of I is stored * P and I actually represent the same memory space */system ("pause ");}
Value exchange
# Include <stdio. h>
# Include <stdlib. h>
// The parameters I and j are equivalent to re-opening two memory addresses. They have nothing to do with the real parameter I and j.
Void swap (int I, Int J ){
Int temp;
Temp = J;
J = I;
I = temp;
}
// P, Q, I, and J indicate the same memory address.
Void swap_pointer (int * P, int * q ){
Int temp;
Temp = * q;
* Q = * P;
* P = temp;
}
Main ()
{
Int I = 10;
Int J = 30;
Printf ("before switching... \ n ");
Printf ("I = % d \ n", I );
Printf ("J = % d \ n", J );
// Swap (I, j );
Swap_pointer (& I, & J );
Printf ("after switching... \ n ");
Printf ("I = % d \ n", I );
Printf ("J = % d \ n", J );
System ("pause ");
}
Common Errors in pointers
# Include <stdio. h>
# Include <stdlib. h>
Main ()
{
/** Spam pointer, wild pointer */
// Int * P; // P does not point to any memory space and cannot be used
// Printf ("* P = % d \ n", * P );
// * P = 2425;
/** Different types of pointers cannot be converted to each other */
Int I = 9999;
Char * C;
C = & I;
Printf ("* c = % C \ n", * C );
System ("pause ");
}
Pointer variables that have been returned cannot be used.
# Include <stdio. h>
# Include <stdlib. h>
// When the function is complete, I will be recycled.
Void func (INT ** q) {// represents the pointer variable of the pointer variable
Int I = 3;
* Q = & I; // * q indicates the pointer Variable P.
}
Main ()
{
Int * P;
Printf ("P memory address: % # x \ n", P );
Printf ("I = % d", * P); // Note: The value may be obtained when I is not recycled.
System ("pause ");
}
Pass pointer variables so that the sub-function can modify the value of the variable in the main function
# Include <stdio. h> # include <stdlib. h> // you can modify the value of the Variable Void func (int * P, int * q) in the main function by passing pointer variables. {* P = 100; * q = 200;} Main () {int I = 10; Int J = 20; func (& I, & J); printf ("I = % d \ n ", i); printf ("J = % d \ n", J); System ("pause ");}
String and array
# Include <stdio. h>
# Include <stdlib. h>
/**
An array is a continuous memory space.
The array name indicates that the first address of the array is the same as the memory address of ARR [0 ].
The memory addresses of subsequent elements are all moved after the first address.
Arr [0] = * (ARR + 1)
*/
// Print every element in the array
Void printarr (int * arr, int Len)
{
Int I = 0; // The c99 version must be initialized outside.
For (; I <Len; I ++)
{
Printf ("arr [% d] = % d \ n", I, * (ARR + I ));
}
}
Main ()
{
// String Array
Char Carr [20] = {'A', 'B', 'C', '\ n '};
Printf ("Carr = % s \ n", Carr );
Printf ("Carr = % # x \ n", & Carr );
Printf ("& Carr [0] =%# x \ n", & Carr [0]);
Printf ("Carr [1] = % C \ n", * (CARR + 1 ));
// Replace the string with the char pointer type
// Char * arr1 = "ABC \ n ";
// Printf ("arr1 = % s", arr1 );
Int arr [5] = {1, 2, 3 };
Printf ("arr = % # x \ n", arr );
Printf ("& arr [0] = % # x \ n", & arr [0]);
Printf ("arr [1] = % d \ n", * (ARR + 1 ));
Printarr (ARR, 5 );
System ("pause ");
}
Pointer operation
# Include <stdio. h> # include <stdlib. h>/** the pointer operation does not move one byte up or down, but moves the number of bytes of the specified type according to the agreed type */main () {// the char type occupies one byte in the memory. Char chararr [4] = {'A', 'B', 'C', 'D '}; char * CP = & chararr [2]; printf ("* (CP-1) = % C \ n", * (CP-1 )); // int type occupies 4 bytes in memory int intarr [4] = {1, 2, 3, 4}; int * IP = & intarr [2]; printf ("* (IP-1) = % d \ n ", * (IP-1); System (" pause ");}
Number of bytes of the pointer
# Include <stdio. h> # include <stdlib. h> main () {int I = 2; float F = 2; double D = 2; char c = 'B'; int * IP = & I; float * fp = & F; double * dp = & D; char * CP = & C; printf ("the length of the int pointer is % d \ n ", sizeof (IP); printf ("float pointer occupies % d \ n", sizeof (FP )); printf ("the length of the double pointer is % d \ n", sizeof (DP); printf ("the length of the char pointer is % d \ n ", sizeof (CP )); /** the length of the int pointer is 4 float pointer to 4 double pointer to 4 char pointer to 4 * // ** 32-bit Operating System the 32-bit pointer with a maximum memory space of 2 only needs 4 bits to indicate all the 64-bit memory space operating systems, when the compiler supports 64-bit, the pointer length is 8 bytes */system ("pause ");}
Function pointer
# Include <stdio. h>
# Include <stdlib. h>
Int add (int x, int y)
{
Printf ("x + y = % d \ n", x + y );
}
Main ()
{
// Function pointer
INT (* pfunc) (int x, int y );
Pfunc = add;
Pfunc (4, 9 );
System ("pause ");
Return 0;
}