A brief summary of the various types of Pointers in the C language

  • 2020-05-09 18:58:29
  • OfStack

There is a lot about the use of Pointers in C language, which is also the soul of C language, and there are a lot of concepts about Pointers in C language, here we learn and summarize some concepts we know.
 
Constant pointer:
First of all, it's a pointer, and a constant is just an attribute used to modify a pointer. It is defined as follows:


char const * cp; 
char a='a'; 

How do you tell? According to the right combination first, first * first, so this cp variable is 1 pointer, and then the const modifier *, so this is 1 constant pointer. A pointer to a constant.


cp=&a; // Normal grammar  
*cp=a; // Incorrect syntax because the value it points to is 1 A constant  

 
Pointer constant:
First of all, it is a constant, pointer is used to modify the constant, that is, the value of the constant is 1 pointer address.


 
char * const cp; 
char a='a'; 

How do you tell? According to the right binding first, const first, so this cp variable is a constant, and then * modifies const, so this is a pointer constant.


cp=&a; // Incorrect syntax because its address is 1 A constant 
*cp=a; // Correct, what the address points to is 1 Common characters 

 
Pointer array:
First of all, it is an array. Pointers are used to modify the contents of an array. What kind of array is it


char *arr[3] = {"1","123","345"}; 

How do you recognize that since [] has a priority greater than *, it is first defined as an array and then modified by *


printf("arr0%c\n",*arr[0]); 
printf("arr1%s\n",arr[1]); 

 
Array pointer:
First of all, it's a pointer, and an array is a pointer to a pointer, a pointer to an array.


char (*p)[3];  // Cannot initialize at the same time  
char arr[3] = {'1','4','7'}; 
p=&arr; // It points to the first address of the array, and the pointer is of type char * [3]  Of type, that is, plus 1 After the operation for sizeof(char [3])3 A number of bytes  

How to identify: first pointer, then [] modifies pointer, because 1 display priority is added this time


printf("%c\n",(*p)[0]);  // First take arr The first address of, and then the contents of the array according to this address  
printf("%c\n",(*p)[1]); 
printf("%c\n",(*p)[2]); 
printf("%c\n",*((char*)p+0)); // To convert char Pointer, evaluate again  
printf("%c\n",*((char*)p+1)); 
printf("%c\n",*((char*)p+2)); 
printf("%c\n",((char*)p)[0]); // To convert char Pointer, take the value of the array, and number 1 A similar  
printf("%c\n",((char*)p)[1]);  
printf("%c\n",((char*)p)[2]);  

Function pointer:
First of all, it's a pointer, and the function modifies the pointer, the pointer to the function.


char (*func)(void); // Define function pointer  
char test(void)  
{ 
return 'A'; 
} 
func = test;  // Initialize the assignment  
printf("test address: %p\n",test); 
printf("func address: %p\n",func); 
char ch = func();  // call  
printf("%c\n", ch); 

How to identify, like array pointer 1, because of the priority of (), this definition is first 1 pointer, and then the description of the pointer, that is, 1 pointer to a function, and the function to which it refers is also specified: that is, what is returned is a character type, and no parameters need to be passed in
 
 
Pointer function:
First it is a function, pointer modifies the return type of the function, that is, a function that returns a pointer
 


char *func(void); 

How to identify, because there is no arc expansion, the priority of * is not as high as that of the arc expansion on the right, so first 1 function is specified, * only modifies the return value


cp=&a; // Normal grammar  
*cp=a; // Incorrect syntax because the value it points to is 1 A constant  

0

 
 
Structure pointer:
Of course, it was originally a pointer, just pointing to the structure. It is therefore a pointer to a structure.

 
Pointer structure:
Pointer structs, which don't necessarily have this concept, are nothing more than structures with Pointers as children.

 
Pointer type conversion:
Pointer type conversion is an interesting thing. You can convert an int pointer to char, and then an char pointer to int. It's like converting 1 between normal characters and int. However, the value of the pointer does not change after conversion, and the only thing that changes is the step size of the pointer, that is, the calculation method when the pointer operation is carried out. When it is an char pointer, its unit of operation is 1 byte, while when it is an int pointer, its unit of operation is usually 4 bytes.
 
Pointer arithmetic:
According to the introduction of pointer type conversion above, different pointer types have different calculation methods in arithmetic operation, and the difference lies in the different step size of bytes. The specific step size of several bytes is determined by the pointer type, and the pointer step size of char is 1. Common pointer operations include pointer and number addition and subtraction operations, the same type of pointer subtraction operations, but also points to the same array, otherwise it does not make much sense. Similarly, it makes less sense to push different types of Pointers to perform operations, or even to report errors.
Here is an example of pointer arithmetic where swapping the values of two variables does not make use of additional variables
After all, the new keyword still applied for extra memory, although there were no applied variables, the soup did not change


cp=&a; // Normal grammar  
*cp=a; // Incorrect syntax because the value it points to is 1 A constant  

1

You can just swap variables:


cp=&a; // Normal grammar  
*cp=a; // Incorrect syntax because the value it points to is 1 A constant  

2

 
Pointer parameters:
Pointer parameter is refers to, pointer as a function of parameters are passed, because C language support only ChanXiangChuan value, and the return value is a value type, so want to return multiple contents from a function or a common data operations with the function body area, that this time can consider to pass through the pointer parameter. The pass pointer is also a pass value, except that the value is the address to which the pointer points, which is an int integer. A common region can be manipulated and data Shared by passing one address.
 
Pointer pointer:
A pointer to a pointer is a pointer to a pointer, and likewise a pointer to a pointer; But the thinking of 1 person can do 2, 3 levels of pointer is very good. Here is the main extension of 1 pointer and multidimensional array association. The processing of multi-dimensional arrays that are decomposed from individual to 1. I'm sure I'm using a 2-dimensional array as an example here, and that extends to a multi-dimensional array.


Related articles: