·
Pointer
Pointer: The pointer is the address
Pointer variable: The variable that holds the variable address
Purpose of using pointers: to be able to find the variable being referred to by a pointer, or to indirectly access the indicated variable through a pointer
Take a variable; stack area
int *p pointer to type int
%p Output pointer
& Address Identifier
Initialization
int *p=null; Must initialize
Put * (*P=&A) when defined, cannot be placed when assigned (P=&A)
* Role of type description
The effect of indirect access to the value that the pointer points to memory
p=&a; Print: P is address, *p value is a
pointer of type void
You can point to any variable, but when you use it, you need to force the type conversion to use
void *p=&a;
printf ("%d", * (int*) p);
Pointer size: all 8 bytes
Pointer arithmetic: can only add and subtract, cannot multiplication
P+n = p+n*sizeof (point to type)
Pointer Access array
1, the array name is actually a pointer to the first element in the array
2. Add a & and p=&a[1]; in front of the actual array element
3. Add an offset to the array name and change the pointer to the array element
* (p+n) = array[n] = * (array + n);
P[0] = = Array[0]; You can do this.
Access an array of two ways:
The subscript method accesses the array name [subscript] pointer [subscript]
Const pointer
Const int* p Pointer to a variable that cannot be changed, can be modified to point to, and its point to memory is immutable by the const modifier
(Const on * left, modifier int)
int const* P-value is not variable
int *const P (const on * right, modifier pointer) pointer cannot change point to
const int *const p can only be initialized and cannot be changed
Const on the left of the asterisk:
Modifies the memory that the pointer points to
1. The pointer can be changed to point
2. You cannot change the value of the memory it refers to by using the pointer
Const to the right of the asterisk:
The modifier is the pointer variable itself
1. The pointer cannot be changed to point
2. You can change the value of the memory it refers to by using the pointer
Const around the asterisk
1. Can not change the point
2. The value cannot be modified by the pointer
Note: The variable itself can still be modified by the variable name
Pointers and strings
Character array representation string
Point to a string representation with the character pointer, pointing to the first address
char* str1 = "Beijing" str1 record only the first address
Beijing exists data area (constant cannot be changed), cannot be changed according to Str1[1]=e;
Strcat cannot be spliced with pointers.
strcpy
Pointers and functions
Pointer variables act as function arguments: pass pointer variables to functions as arguments, and address passes, so you can change the value of an argument in a function
Array name as argument and formal parameter of function: Address passing
The return value of the function is a pointer: The function's return value type must be defined as a pointer type
function pointers: pointers to functions
Data type (* pointer variable name) (parameter list)
Int (*p) (int a)--int (*) (int a) points to a function pointer that returns a value of int
The return value of int *p1 (int a)--(int*) (int a) is the int* function. The function is named P1
Parentheses cannot be saved
int * (*P2) (int*a,int*b)----point to the return value of int*, the parameter is: The function pointer
A description of the pointer variable that points to the function:
1. When assigning the first address of a function to a pointer variable, write the function name directly, without having to write parentheses and parameters
2. When invoking a function with a pointer variable, specify the actual parameters of the function
P=func1;p () = = Func1 (20)
The function name is the first address of the function.
Indicates that the function pointer p points to func1
Pointer access to a two-dimensional array
int a[3][4];
int (*p) [3][4];
p=&a; *p = = A;
(*p) [1] [1];
P[0] ==*p;
P[0][0][0]=A[0][0]
P[0][1]=A[1];
Pointers to pointers (two-dimensional pointers)
If a pointer variable holds the address of another pointer variable, the pointer variable is called a pointer variable pointing to the pointer.
int A;
int *p=&a;
int **p=&p;
**p = = A
Pointer to array
Definition: type name (* pointer name) [array length]
such as: int A[5]={1,1,1,1,1};int (*P) [5]=&a;
The definition is an array of pointers without parentheses, because [] precedence is higher than *.
Array length when the array pointer is defined, the element type must be the same as the array pointed to
int (*p) [5]; pointer to an integer array with an array length of 5
char* (*P1) [10]; Pointer to an array of type char* [10]
int *p2[10]; an array of type int* [10]
The array pointer points to the entire array, not a single element
Array pointer plus 1, equivalent to P+sizeo (array name)
Access to array elements: a[0],*a,* (a+1), (*p) [0],p[0][0] (*p) ==a, p[0]=a;
Pointer (array)
If the array element is a pointer variable, make an array of pointers
General form: type name * array name [array length]
int* a[10]; Each element is a int* pointer
"C Language" basis (ix) pointers