C Language Notes (pointer) and C language pointers
Pointer
1. Basic pointer variable
(1) Definition
Int I, j;
Int * pointer_1, * pointer_2;
Pointer_1 = & I;
Pointer_2 = & j;
Equivalent
Int * pointer_1 = & I, * pointer_2 = & j;
(Pointer misunderstanding: You should first know that the pointer is an address and is immutable. the pointer variable (the above-defined pointer_1 is a pointer variable) is a variable and the variable is variable, compared with common variables, the amount of CPU resources it stores will be processed as addresses)
-------------------------------------------------------
(2) Note
*: Take the storage volume of the address space. * The content following the CPU is processed as the address;
&: Get the address of the variable, & the content following the CPU is processed as the variable;
2. pointer variables of one-dimensional arrays
(1) Definition
Int a [10];
Int * p;
P = & a [0];
Equivalent to p =;
Both a [0] And a indicate the first address of the array;
(2) pointer variables pointing to Arrays can also carry subscript, such as p [I] And * (p + I );
(3) array name a represents the address of the first element of the array. It is a pointer constant and its value remains unchanged during the program running. Do not make any errors like a ++.
3. When using an array name as a function parameter, the address of the first element of the array is passed to the function. Therefore, the parameter must be a pointer variable;
Summary:
①: Both the form parameter and the real parameter use the array name.
Int a [10];
F (a, 10 );
.
.
.
Viod (int x [], n)
{...}
Simple Solution: When the compiler encounters an object named array, the object will be treated as a pointer variable. Therefore, the object points to each element of array a [] during the program running, it can also be understood that the real parameters occupy the same memory space in the running interval;
② Real parameter array name, parameter pointer variable
Int a [10];
F (a, 10 );
.
.
.
Viod (int * x, n)
{...}
Simple solution: the function starts to execute x pointing to each element of the array.
③ All real parameters are pointer variables.
Int a [10], * p =;
F (p, 10 );
.
.
.
Void f (int * x, int n)
{...}
Introduction: 1. p points to the first element of array a; 2. p points to x; 3. x points to each element of array;
④ The real parameter is a pointer variable and the form parameter is an array name
Int a [10], * p =;
F (p, 10)
Void f (int x [], int n)
{...}
Simple Solution: 1. Processing the form parameter as a pointer variable, followed by ③
4. Properties of array
Int a [3] [4] = {1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19 }}
Int data occupies 2 bytes of memory in the KEIL compiling environment
Representation |
Description |
Address |
A |
Two-dimensional array name, pointing to an array a [0], that is, the first address of line 0 |
Set 2000 |
A [0], * (a + 0), * |
0 rows, 0 columns, element addresses |
2000 |
A + 1, & a [1] |
1 row, 0 columns, element address |
2008 |
A [1], * (a + 1) |
Address of element a [1] [0] In column 0 of a row |
2008 |
A [1] + 2, * (a + 1) + 2, & [1] [2] |
Element address of one row and two columns (that is, a [1] [2 ]) |
2012 |
* (A [1] + 2), * (a + 1) + 2), a [1] [2] |
Element values of one row and two columns (that is, a [1] [2 ]) |
Element 13 |
(1) why * (a + 1) indicates the first address of the first line?
A: * (a + x) = a [X]; the two are equivalent.
(2) How does the C language make * (a + x) completely equivalent to a [X?
A: In an array, * a is a [0], a + 1 points to a [1], and a + 2 points to a [2]. a + 3 points to a [3], that is
* (A + 1), * (a + 2), and * (a + 3) are respectively a [1], a [2], and a [3]. In the relationship between actual code generation and mechanical code, the two effects are completely equivalent.
5. Interpretation of int (* p) [4]
A: int (* p) [4] indicates that p is a pointer variable pointing to a one-dimensional array containing four integer elements, in this case, p can only point to a one-dimensional array containing four elements. p cannot point to an element in a one-dimensional array. The value of p is the starting address of the one-dimensional array.
P
(* P) [0] |
(* P) [1] |
(* P) [2] |
(* P) [3] |
→
It can be concluded that
Pointer as function parameter |
① Pointer variable pointing to the variable (General) |
② PointOne-dimensionalArray pointer variable |
6. while (* from! = '\ 0') = while (* from! = 0). When the CPU is processing, the characters and ASCLL codes are processed in the same way.
7. Character arrays and character pointer Variables
Char str [14];
Str = "I Love China"; × (assign values to each element only for character arrays)
An array can only assign an initial value to the whole when it is defined, but it cannot assign a value to the whole in the assignment statement;
BUT (aber)
Char *;
A = "I Love China"; √ (note that the address assigned to a is not a character, but the address of the first element of the string)
Equivalent
Char * a = "I Love China ";
/****** Typical printf ******/
Char * format;
Format = "a = % d, B = % f \ n"; when the string is processed
Printf (format, a, B );
Equivalent
Printf ("a = % d, B = % f \ n", a, B );
8. pointer to function
# Include <stdio. h>
Void main ()
{
Int max (int, int );
Int (* p) (int, int );
Int a, B, c;
P = max;
Scanf ("% d, % d", & a, & B );
C = (* p) (a, B );
Printf ("a = % d, B = % d, max = % d \ n", a, B, c );
}
(1), "int (* p) (int, int);" is used to define p as a pointer variable pointing to the function. * brackets at both ends of p cannot be omitted.
(2) "p = max" assigns the entry address of function max to the pointer Variable p. Similar to the array name representing the address of the first element of the array,The function name indicates the function entry address.. (Likewise, max ++ is incorrect)
(3) The general definition form of the pointer variable pointing to the function:
Data Type (* pointer variable name) (function parameter table column );
9.Use Pointer to function as function parameter
For example:
Int a = 1;
Int B = 2;
Int max (int x, int y)
{______________}
Void process (int, int, int (* fun) (int, int); // Declaration
Process (a, B, max); // reference
10. functions that return pointer values
Type name * function name (parameter list );
For example:
Int * a (int x, int y );
Explanation: The operation Priority of parentheses is greater than *, and the return value of function a (int x, int y) is the address value;
Please distinguish int (* p) (int, int) in question 8; this is a pointer to the function;
Int (* p) (int, int); // pointer to the Function
Int * a (int, int); // function that returns the pointer Value
The order of the two is the opposite.
Usage:
Float * p;
Float score [] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {11, 12, 13, 14 }};
Float * search (float (* pointer) [4], int n );
P = search (score, m );
11. pointer Array
(1) definition form
Type name * array name [array length];
For example, int * p [4];
Do not write it as int (* p) [4]; this is a pointer variable pointing to a one-dimensional array;
Each volume in the pointer array is used as a pointer variable;
Why use a pointer array?
A: It can be used to point to several strings, Making string processing more convenient and flexible.
12. pointer to pointer
(1) Definition
Char ** p;
Note: * p is the address of ** p, p is the address of ** p, and ** p is the variable. Both p and * p are addresses;
There are two * signs in front of p. * The concatenation of operators is from right to left, so ** p is equivalent to * (* p). Obviously * p is the definition form of pointer variables.
13. NULL
# Define NULL 0 (this sentence already exists in stdio. h)
P = NULL;
It indicates that p does not point to any useful unit. It should be noted that the value of p is NULL and the value of p is not assigned to p. The former has a value (0) and does not point to any program variable. Although the latter has not assigned a value to p but is not equal to P, its value is an unpredictable value, that is, p may point to an unspecified unit. This situation is very dangerous. Therefore, assign a value to the pointer variable before referencing it.
Any pointer variable or address can be compared with NULL for equality or inequality. For example:
If (p = NULL )...
14. pointer operation to the same Array
(1) subtraction and Comparison
A [0] |
A [1] |
A [2] |
A [3] |
A [4] |
P1 points to a [1], p2 points to a [3]
So:
Subtraction: p2-p1 = 2, but p1 + p2 is meaningless;
Comparison: the pointer variable pointing to the previous element "less than" points to the pointer variable of the subsequent element.
That is, p1 <p2, or "p1 <p2" is 1 (true );
15. void pointer type
Char * p1;
Void * p2;
.
.
.
P1 = (char *) p2;
You can also:
P2 = (void *) p1;
Void is used to point to an abstract type data. When assigning its value to another pointer variable, a forced type conversion is required to make it suitable for the type of the variable to be assigned.
15. pointer type Summary
Definition |
Description |
Int I; |
Define integer variable I |
Int * p; |
P is the pointer variable pointing to integer data. |
Int a [n]; |
Defines integer array a, which has n elements |
Int * p [n]; |
Defines the pointer array p, which has n pointer elements pointing to integer data (multiple) |
Int (* p) [n]; |
P is the pointer variable pointing to a one-dimensional array containing n elements (1) |
Int f (); |
F is a function that returns an integer value. |
Int * p (); |
P is a function that returns a pointer pointing to integer data. |
Int (* p )(); |
P is the pointer to the function. This function returns an integer value. |
Int ** p; |
P is a pointer variable that points to an integer Data Pointer variable. |
Next update time:
New understandings and insights can be shared and learned together
Notes on C pointer Basics
P1 p2 has no relation. After the preceding statement is executed, p1 p2 is two different pointers, but the two pointers point to the same content, that is, integer.
C language: Write a pointer to search for a specified value pointer in an array
Thank you very much.
For functions: int * find (int a [], int value), where a is the first address of the integer array, and value is the tested value. We can use pointer a to indirectly reference the value of the first element of the array and compare it with the value. After comparison, move the pointer backward, then compare the values of the next element by indirect reference until the comparison of all elements in the tested value or integer array is completed.
So here is a question: how do we know when the integer array will end with a pointer? There is no way for an integer array, so we must first know the length of the integer array, which can be given in the parameter or in the main function definition.
The procedure is as follows:
Int * find (int a [], int value)
{
Int I = 0, * p =;
For (I = 0; I <= 9; I ++)
{
If (* p = value)
Return p;
Else
P ++;
}
If (I> = 10)
Return 0;
}
Main ()
{
Int c [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0 };
Int n;
Scanf ("% d", & n );
Printf ("% x % d", find (c, n), * (find (c, n);/* The first is the return address of the function, the second is the value in the return address of the function */
Getch ();
}
In addition, we test whether a program or function is correct depends not on whether it runs correctly, but on whether the running result meets our expectations. The test method is as follows:
Enter any number. If this number is in array C, check whether the number of hexadecimal addresses and the number of addresses are the same as the number of input values. If it is not in array C, check whether the return value is 0.