C language 06 pointer advanced
1. pointer usage 1.1 Problems
Write a program, review the basic application of the pointer, test the NULL pointer and the wild pointer, and add the condition judgment to avoid it.
Step 1
To implement this case, follow these steps.
Step 1: The usage code of the pointer is as follows:
#include
void swap (int * a, int * b)
{
int tmp = * a;
* a = * b;
* b = tmp;
}
int main ()
{
int a = 10;
int * p = & a;
printf ("a =% d \ n", * p);
printf ("p =% p \ n", p);
p = p + 1;
printf ("p + 1 =% p \ n", p);
p = p-1;
printf ("p-1 =% p \ n", p);
int b = 20;
printf ("a =% d, b =% d \ n", a, b);
swap (& a, & b);
printf ("a =% d, b =% d \ n", a, b);
const int * p1 = & a;
// * p1 = 30;
p1 = & b;
int * const p2 = & a;
* p2 = 40;
// p2 = & b;
p = NULL;
if (p)
printf ("p =% p \ n", p);
return 0;
}
In the above code, the following code:
void swap (int * a, int * b)
{
int tmp = * a;
* a = * b;
* b = tmp;
}
A function swap1 is defined. The function has two parameters, which are integer pointer variables a and b. In this function, the following statement:
int tmp = * a;
* a = * b;
* b = tmp;
Through the third variable tmp, the values of the addresses pointed to by integer pointer variables a and b are exchanged.
In the above code, the following code:
int a = 10;
int * p = & a;
printf ("a =% d \ n", * p);
First, define an integer variable a and initialize it to 10.
Then, define an integer pointer variable p and initialize it to the address of variable a.
Finally, the content of the address pointed to by the pointer variable p is output.
In the above code, the following code:
printf ("p =% p \ n", p);
p = p + 1;
printf ("p + 1 =% p \ n", p);
First, output the address value before the pointer variable plus 1.
Then, increment the pointer variable p.
Finally, the address value after incrementing the pointer variable is output.
In the above code, the following code:
p = p-1;
printf ("p-1 =% p \ n", p);
First, decrement the pointer variable p by 1.
Then, the address value of the pointer variable minus 1 is output.
In the above code, the following code:
int b = 20;
printf ("a =% d, b =% d \ n", a, b);
swap (& a, & b);
printf ("a =% d, b =% d \ n", a, b);
First, define an integer variable b and initialize it to 20.
Then, output the values of variables a and b before calling the swap function.
Next, call the function swap.
Finally, output the values of variables a and b after calling the swap function.
In the above code, the following code:
const int * p1 = & a;
// * p1 = 30;
p1 = & b;
Adding the const keyword to the left of the * sign locks the content of the address pointed to by the pointer p1, so line 2 is wrong and line 3 is correct.
In the above code, the following code:
int * const p2 = & a;
* p2 = 40;
// p2 = & b;
Adding the const keyword to the right of the * sign locks the pointer p1 itself, so line 2 is correct and line 3 is wrong.
In the above code, the following code:
p = NULL;
if (p)
printf ("p =% p \ n", p);
To prevent the appearance of wild pointers, after the pointer p is no longer used, it should be set to null, so that whether the pointer is valid can be judged by whether the condition p is null.
1.3 Complete code
The complete code for this case is as follows:
#include
void swap (int * a, int * b)
{
int tmp = * a;
* a = * b;
* b = tmp;
}
int main ()
{
int a = 10;
int * p = & a;
printf ("a =% d \ n", * p);
printf ("p =% p \ n", p);
p = p + 1;
printf ("p + 1 =% p \ n", p);
p = p-1;
printf ("p-1 =% p \ n", p);
int b = 20;
printf ("a =% d, b =% d \ n", a, b);
swap (& a, & b);
printf ("a =% d, b =% d \ n", a, b);
const int * p1 = & a;
// * p1 = 30;
p1 = & b;
int * const p2 = & a;
* p2 = 40;
// p2 = & b;
p = NULL;
if (p)
printf ("p =% p \ n", p);
return 0;
}
2 Application of secondary pointers 2.1 Problems
Use secondary pointers to manipulate arrays of strings.
2.2 Procedure
The realization of this case requires the following steps.
Step 1: Application of second-level pointers (pointer array: essentially an array, storing pointers)
The code is as follows:
#include
int main ()
{
char * name [] = {"zhangsan", "lisi", "wangwu", "zhaoliu"};
char ** p = name;
for (int i = 0; i <4; i ++)
printf ("% s \ n", p [i]);
return 0;
}
In the above code, the following code:
char * name [] = {"zhangsan", "lisi", "wangwu", "zhaoliu"};
Define a pointer array name and initialize it to four strings.
In the above code, the following code:
char ** p = name;
Define a secondary pointer p and initialize it to the pointer array name name.
In the above code, the following code:
for (int i = 0; i <4; i ++)
printf ("% s \ n", p [i]);
Set the loop to output the contents of the character pointer array pointed to by the secondary pointer p. Among them, p is equivalent to name, p [i] is equivalent to name [i]. 2.3 Complete code
The complete code for this case is as follows:
#include
int main ()
{
char * name [] = {"zhangsan", "lisi", "wangwu", "zhaoliu"};
char ** p = name;
for (int i = 0; i <4; i ++)
printf ("% s \ n", p [i]);
return 0;
}
3 Application of secondary pointers (continued 1) 3.1 Questions
Define a function that requires string conversion to int, character-to-character conversion, and if it encounters a non-numeric character, return the previous integer and the following string.
3.2 Procedure
The realization of this case requires the following steps.
Step 1: The code for the application of the secondary pointer (continued 1) is as follows:
#include
int convert (char ** str)
{
int num = 0;
while (** str)
{
if (** str> = '0' && ** str <= '9')
num = num * 10 + ** str-'0';
else
break;
(* str) ++;
}
return num;
}
int main ()
{
char str [] = "1234abc";
char * p = str;
printf ("% d \ n", convert (& p));
printf ("% s \ n", p);
return 0;
}
In the above code, the following code:
int convert (char ** str)
{
int num = 0;
while (** str)
{
if (** str> = '0' && ** str <= '9')
num = num * 10 + ** str-'0';
else
break;
(* str) ++;
}
return num;
}
Define a function convert to convert the leading numeric characters in the string to numbers and output the following string. In this function, the following code:
int num = 0;
Define an integer variable num to store the converted number. In this function, the following code:
if (** str> = '0' && ** str <= '9')
num = num * 10 + ** str-'0';
Determine whether the character ** str is a numeric character, and if so, convert it. In this function, the following code:
while (** str)
{
if (** str> = '0' && ** str <= '9')
num = num * 10 + ** str-'0';
else
break;
(* str) ++;
}
Set up a loop to check each character in the string * str one by one, and convert it if it is a numeric character. Otherwise, exit the loop. In this function, the following code:
return num;
Returns, the converted number.
In the above code, the following code:
int main ()
{
char str [] = "1234abc";
char * p = str;
printf ("% d \ n", convert (& p));
printf ("% s \ n", p);
return 0;
}
First define a character array str, and initialize a string.
Then, define a character pointer p and initialize it to a character array str.
Next, print the converted numbers.
Finally, print the remaining characters.
3.3 Complete code
The complete code for this case is as follows:
#include
int convert (char ** str)
{
int num = 0;
while (** str)
{
if (** str> = '0' && ** str <= '9')
num = num * 10 + ** str-'0';
else
break;
(* str) ++;
}
return num;
}
int main ()
{
char str [] = "1234abc";
char * p = str;
printf ("% d \ n", convert (& p));
printf ("% s \ n", p);
return 0;
}
4 Basic usage of void * 4.1 Problem
Define an int *, then assign it to void *, and test the basic usage of void *.
4.2 Procedure
The realization of this case requires the following steps.
Step 1: Basic usage of void *
The code is as follows:
#include
int main ()
{
int a = 10;
int * p = & a;
void * p1 = p;
// * p1 = 20;
* (int *) p1 = 20;
printf ("a =% d \ n", * (int *) p1);
return 0;
}
In the above code, the following code:
int a = 10;
Define an integer variable a and initialize it to 10.
In the above code, the following code:
int * p = & a;
Define an integer pointer variable p and initialize it to the address of the integer variable a.
In the above code, the following code:
void * p1 = p;
Define a universal pointer p1 and initialize it to the value of the integer pointer p. A universal pointer is a pointer that can point to a variable of any data type.
In the above code, the following code:
// * p1 = 20;
The universal pointer cannot directly use * to access the content of the address pointed to by the pointer.
In the above code, the following code:
* (int *) p1 = 20;
printf ("a =% d \ n", * (int *) p1);
The universal pointer must be converted to the data type it points to before the content of the address pointed to by the pointer can be accessed with *.
4.3 Complete code
The complete code for this case is as follows:
#include
int main ()
{
int a = 10;
int * p = & a;
void * p1 = p;
// * p1 = 20;
* (int *) p1 = 20;
printf ("a =% d \ n", * (int *) p1);
return 0;
}
5 Use of function pointers 5.1 Problems
Use function pointers Realize different processing methods for a set of data.
5.2 Procedure
The realization of this case requires the following steps.
Step 1: The code for using the function pointer is as follows:
#include
int min (int * buf, int size)
{
int min = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i] <min)
min = buf [i];
return min;
}
int max (int * buf, int size)
{
int max = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i]> max)
max = buf [i];
return max;
}
int main ()
{
int data [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int (* p) (int *, int);
p = min;
printf ("The minimum value in data is% d \ n", p (data, 10));
p = max;
printf ("The maximum value in data is% d \ n", p (data, 10));
return 0;
}
In the above code, the following code:
int min (int * buf, int size)
{
int min = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i] <min)
min = buf [i];
return min;
}
Define a function min for finding the minimum value in a set of numbers. The function has two parameters, described as follows:
The first parameter is an array containing a set of numbers.
The second parameter is the number of arrays.
In this function, the following statement:
int min = buf [0];
Define an integer variable min for saving the minimum value in a group of numbers. In this function, the following statement:
for (int i = 0; i <size; i ++)
if (buf [i] <min)
min = buf [i];
Traverse the array to find the minimum value. In this function, the following statement:
return min;
Returns the minimum value.
In the above code, the following code:
int max (int * buf, int size)
{
int max = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i]> max)
max = buf [i];
return max;
}
Define a function max for finding the maximum value in a group of numbers. The function has two parameters, described as follows:
The first parameter is an array containing a set of numbers.
The second parameter is the number of arrays.
In this function, the following statement:
int max = buf [0];
Define an integer variable max to save the maximum value in a group of numbers. In this function, the following statement:
for (int i = 0; i <size; i ++)
if (buf [i]> max)
max = buf [i];
Traverse the array to find the maximum value. In this function, the following statement:
return max;
Returns the maximum value.
In the above code, the following code:
int main ()
{
int data [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
In the main function, define an integer array data and initialize it to 10 integers.
In the above code, the following code:
int (* p) (int *, int);
Define a function pointer. The function pointed to by this pointer must return an integer and contain two parameters, one is an integer pointer variable and the other is an integer variable.
In the above code, the following code:
p = min;
Assign the function name min to the function pointer p.
Note: When assigning values, the return value and parameter types of the function corresponding to the function name must be consistent with the requirements of the function pointer.
In the above code, the following code:
printf ("The minimum value in data is% d \ n", p (data, 10));
Use the function pointer p to call the function min.
In the above code, the following code:
p = max;
Assign the function name max to the function pointer p.
In the above code, the following code:
printf ("The maximum value in data is% d \ n", p (data, 10));
Use the function pointer p to call the function max.
5.3 Complete code
The complete code for this case is as follows:
#include
int min (int * buf, int size)
{
int min = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i] <min)
min = buf [i];
return min;
}
int max (int * buf, int size)
{
int max = buf [0];
for (int i = 0; i <size; i ++)
if (buf [i]> max)
max = buf [i];
return max;
}
int main ()
{
int data [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int (* p) (int *, int);
p = min;
printf ("The minimum value in data is% d \ n", p (data, 10));
p = max;
printf ("The maximum value in data is% d \ n", p (data, 10));
return 0;
}
6 Use of function pointers (continued 1) 6.1 Problem
Sort by different sorting rules.
6.2 Procedure
The realization of this case requires the following steps.
Step 1: In the main program, define an array
code show as below:
#include
int main ()
{
int a [10] = {1,2,3,4,5,6,7,8,9,0};
return 0;
}
Step 2: Define array printing function
code show as below:
#include
void print (int * a, int n)
{
for (int i = 0; i printf ("% d", a [i]);
}
printf ("\ n");
}
int main ()
{
int a [10] = {1,2,3,4,5,6,7,8,9,0};
print (a, 10);
return 0;
}
Step 3: Define a sorting function with sorting rules
The sorting method uses the bubbling method. code show as below:
#include
void print (int * a, int n)
{
for (int i = 0; i printf ("% d", a [i]);
}
printf ("\ n");
}
int rule1 (int x, int y)
{
return x-y;
}
void sort (int * a, int n, int (* f) (int, int))
{
for (int i = 0; i for (int j = 0; j if (f (a [j], a [j + 1])> 0) {
int t = a [j];
a [j] = a [j + 1];
a [j + 1] = t;
}
}
}
}
int main ()
{
int a [10] = {1,2,3,4,5,6,7,8,9,0};
print (a, 10);
sort (a, 10, rule1);
print (a, 10);
return 0;
}
In the above code, the following code:
int rule1 (int x, int y)
{
return x-y;
}
It is a sorting rule. This function returns the difference between the formal parameters x and y. If the return value is greater than 0, it means that x is greater than y; if the return value is equal to 0, it means that x is equal to y;
In the above code, the following code:
void sort (int * a, int n, int (* f) (int, int))
It is the function header of the sorting function. This function is a function with no return value. The function name is sort. The function has three formal parameters. The first formal parameter is an integer pointer variable, which is used to receive the data to be sorted. The second parameter is an integer variable, which is used to receive the number of data that needs to be sorted; the third parameter is a pointer variable of a function, and int means that the function pointer variable points to the return value of the function as an integer , F is the variable name of the function pointer variable, (int, int) represents that the function pointed to by the function pointer variable has two formal parameters.
In the main function, use the following code to call the sort function:
sort (a, 10, rule1);
Among them, the first actual parameter a is the array name of the array a, the second actual parameter 10 is the array length of the array a, and the third actual parameter rule1 is the function name, which is the sorting rule.
In the above code, the following code:
if (f (a [j], a [j + 1])> 0) {
int t = a [j];
a [j] = a [j + 1];
a [j + 1] = t;
}
The function is called using a function pointer, the pointer variable f is used as a formal parameter, the entry address of the function passed by the actual parameter is received, that is, the rule1 function, and the rule1 function is called using f (a [j], a [j + 1]), where, a [j] and a [j + 1] are the two actual parameters of the function pointed to by the function pointer variable f, namely the two actual parameters of the function rule1.
Step 4: Define other sorting rules function codes are as follows:
#include
void print (int * a, int n)
{
for (int i = 0; i printf ("% d", a [i]);
}
printf ("\ n");
}
int rule1 (int x, int y)
{
return x-y;
}
int rule2 (int x, int y)
{
return y-x;
}
int rule3 (int x, int y)
{
return x% 3-y% 3;
}
void sort (int * a, int n, int (* f) (int, int))
{
for (int i = 0; i for (int j = 0; j if (f (a [j], a [j + 1])> 0) {
int t = a [j];
a [j] = a [j + 1];
a [j + 1] = t;
}
}
}
}
int main ()
{
int a [10] = {1,2,3,4,5,6,7,8,9,0};
print (a, 10);
sort (a, 10, rule1);
print (a, 10);
sort (a, 10, rule2);
print (a, 10);
sort (a, 10, rule3);
print (a, 10);
return 0;
}
6.3 Complete code
#include
void print (int * a, int n)
{
for (int i = 0; i printf ("% d", a [i]);
}
printf ("\ n");
}
int rule1 (int x, int y)
{
return x-y;
}
int rule2 (int x, int y)
{
return y-x;
}
int rule3 (int x, int y)
{
return x% 3-y% 3;
}
void sort (int * a, int n, int (* f) (int, int))
{
for (int i = 0; i for (int j = 0; j if (f (a [j], a [j + 1])> 0) {
int t = a [j];
a [j] = a [j + 1];
a [j + 1] = t;
}
}
}
}
int main ()
{
int a [10] = {1,2,3,4,5,6,7,8,9,0};
print (a, 10);
sort (a, 10, rule1);
print (a, 10);
sort (a, 10, rule2);
print (a, 10);
sort (a, 10, rule3);
print (a, 10);
return 0;
}