The following describes the relationship between pointers and arrays.
Pointer to a one-dimensional array
Pointer to a multi-dimensional array
Pointer to a string
1. Definition of pointer variables pointing to array elements: type * pointer variable name
Example: int * p;
2. Meaning of array name: array name indicates the first address of the array.
3. assign a value to the pointer variable of the array element: pointer variable = address of an array element (usually the first address)
Example: int a [10], * p;
P = a; or p = & a [0];
Figure:
Addition and subtraction of pointer to array
P ++; // p pointer moves p to the type byte, p points to the int type array, so p moves two bytes to a [1]
There are two methods to reference an array:
1. subscript method: for example, a [I]
2. pointer method: for example, * (a + I) or * (p + I)
If an array and pointer are defined as int a [5], * p =;
Subscript Method: a [0] a [1] a [2] a [3] a [4]
Pointer method: * p * (p + 1) * (p + 2) * (p + 3) * (p + 4)
P [0] p [1] p [2] p [3] p [4]
* A * (a + 1) * (a + 2) * (a + 3) * (a + 4)
Calculate the address of the array element using the array name to find the element value.
Main ()
{
Int a [10]; int I;
For (I = 0; I <10; I ++)
Scanf ("% d", & a [I]);
Printf ("\ n ");
For (I = 0; I <10; I ++)
Printf ("% d", * (a + I ));
}
Use Pointer variables to point to array elements:
Main ()
{
Int a [10];
Int * p, I;
For (I = 0; I <10; I ++)
Scanf ("% d", p ++ );
Printf ("\ n ");
For (p = a; p <(a + 10); p ++)
Printf ("% d", * p );
}
Note: p ++ is valid, but a ++ is invalid (a is the array name, representing the first address of the array, which is a constant address)
Pay attention to the current value of the pointer variable.
P ++; (or p + = 1 ;)
P points to the next element, that is, a [1].
X = * p ++; // not recommended
* Similar to ++, it is equivalent to calculating * p first, assigning a [0] to x, and then ++
* (P ++) and * (++ p)
* (P ++) is to first take the value of * p, and then make p + 1;
* (++ P) adds 1 to p first, and then obtains the value of * p.
(* P) ++;
Indicates that the element value pointed to by p is incremented by 1 (that is, a [0] ++), rather than the pointer value plus 1.
Example:
Main ()
{
Int a [3], * p =;
A [0] = 10; a [1] = 20; a [2] = 30;
Printf ("% d/t", * p ++ );
Printf ("% d \ n", * ++ p );
}
Output result: 10 30
Main ()
{
Int a [] = {1, 2, 3, 4, 5 };
Int * p = a + 3;
Printf ("% d, % d, % d \ n", * p, * (p + 1), * p + 3 );
}
Result: 4, 5, 7
Multi-dimensional array pointer
1. The definition reference format is similar to the pointer to a one-dimensional array.
Example: int a [3] [4], * p
P = & a [2] [3];
2. Column and column address Representation
If defined: int a [3] [4];
The number in the table indicates the address value of each element.
Pointer and string
1. String Representation
Use a character array: char string [] = "I Love China! ";
Use a character pointer: the definition of a character pointer variable: char * p;
Value assignment: pointer = address of a character in the string
Char * s = "I Love China! "; // Initialize s
The string ends with a/0 identifier, so the actual length is the string length + 1.