An understanding of Pointers in the C language and their underlying usage instances

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

Pointer to C language, the key meaning is "to point".

What does "point" mean?
It can be understood as an instruction. So let's say I have an object, let's call it A. It is this object, with this name, that we are able to communicate series 1 away from the substance of this object. The indication of an object is the abstraction of the object. With this capacity for abstraction comes wisdom and civilization. So this is the power of the abstract method of "indicating".

Back to the C language pointer, pointer is a 1 segment data/instruction (in the von neumann system, the 2 are interlinked, in the same 1 space) indicator. This is the indication, which is the starting position of the data/instruction. But the data/code requires a method of interpretation. For example, 0x0001, can be used as an integer, can be used as a string instruction, can be used as a string of characters, however you want to interpret it.

The C language, at compile time, determines the "method of interpretation" of this data/instruction.
An integer pointer, for example, means that it can be interpreted from the point to which the pointer p points, as an integer.
A function pointer means that it can be interpreted from the position pointed by p, which can be interpreted as a one-paragraph instruction. The corresponding input, output and return values meet the corresponding requirements according to the type of function pointer.

To sum up, the essence of the C language is the pointer, but the pointer is not only the essence of the C language, it is the essence of abstraction. There are similar things in every language, such as functions, such as references.

(the difference between reference and pointer, my understanding, can not carry out the +/- offset operation of the pointer, is the reference. Random deviation makes it easy to make the target position not conform to its corresponding meaning, resulting in interpretation failure, and then crash. An offset pointer is added to make it easier to represent 1 heap with the same type of data/instructions, such as an array.

The same void type of pointer is also a feature of the C language. The void pointer, which removes the pointer of the specified type so that the pointer can be interpreted in any way, poses the potential problem above. But it is also fair to say that this C language has its own power (which I generally understand as the power of C). The benefits of this are very flexible. Because you can represent all types of data using a single type. The problem is similar to the one above. That is, if it is not interpreted properly, it can have disastrous consequences. The cast of C is also a broken pointer interpretation. It can also cause problems.

Let's take a look at 1 of the basics of Pointers:

1. The basic


int i = 10; 
int *p = &i; /*  define 1 A point to int Pointer to type p And put the i Address to it  */ 
printf("i=%d, &i=%p, p=%p, *p=%d \n", i, &i, p, *p); 

The program output is:


i=10, &i=0x22ac44, p=0x22ac44, *p=10

&i is the address of i, p holds the address of i, *p is the value of the pointer, i.

2. Parameters and return values of pointer types


/*  define 1 The return value is pointing int A function that is a pointer to a type  */ 
int *swap(int *px, int *py) 
{ 
 int temp; 
 temp = *px; 
 *px = *py; 
 *py = temp; 
 return px; 
} 
int main(void) 
{ 
 int i = 10; 
 int j = 20; 
 int *m = swap(&i, &j); 
 printf("i=%d, j=%d, *m=%d \n", i, j, *m); 
 return 0; 
}

 

The program output is:


i=20, j=10, *m=20

return px is equivalent to defining a temporary pointer of type int to hold px, and then assigning this pointer to m, so m points to px just like px does.

Pointers and arrays


int a[5] = {1, 2, 3, 4, 5}; 
int *pa = &a[0]; 
printf("*pa=%d pa=%p a=%p \n", *pa, pa, a); 
pa++; 
printf("*pa=%d \n", *pa); 

The program output is:


*pa=1 pa=0x22ac28 a=0x22ac28
*pa=2

When the array name is passed as an argument, you actually pass a pointer to the first element, as you can see from the input above.
The pointer pa++ makes pa point to the next element.

4. Pointer with const


int n = 30; 
const int *x = &n; 
int const *y = &n; 
printf("*x=%d y++=%p \n", *x, y++); 

Program output:


*x=30 y++=0x22ac1c

const int and int const are like 1, both of which define a pointer to the const int type. So *x is immutable, you can't do things like (*x)++, but x is mutable, you can do x++.


int * const z = &n; 
printf("++(*z)=%d \n", ++(*z)); 

Program output:


++(*z)=31

The above definition is a pointer to const of type int, so z is immutable, but the value of the pointer is variable. To define an immutable pointer:


i=10, &i=0x22ac44, p=0x22ac44, *p=10
0

5. Pointer to pointer


i=10, &i=0x22ac44, p=0x22ac44, *p=10
1

Program output:


i=10, &i=0x22ac44, p=0x22ac44, *p=10
2

*ppc takes the value of pc, and **ppc is the same thing as *pc, which is the value of c.


Related articles: