This article has the following contents :

1. What is a pointer 2. A pointer has a pointer type 2 Meaning 3. Wild pointer
4. Pointer operation 5. Pointer and array 6. Secondary pointer
7. Pointer array
<>1. What is a pointer ?

official language :

In Computer Science , Pointer ( Pointer ) Is an object in a programming language , Use address , Its value points directly to ( points to ) A value stored in another place in computer memory . because
The required variable unit can be found through the address , so to speak , The address points to the variable unit . therefore , Address visualization is called “ Pointer ". It means that you can find the memory unit with its address through it .

In short , A pointer is a variable , The address stored in the variable , therefore , The pointer is **“ address ”**, Take the code as an example , Demonstrate the meaning of the official language ;
#include <stdio.h> int main() { int a = 10; int* p = &a; *p = 20; printf("%d",
a); return 0; } /* Running results 20 */
Pointer variable p It's called a pointer , He is an object ,p The value of points to exist A value in another place in computer memory (a), So when dereferencing p Time ,*p= 20,p Use address
, Make it point to a It became 20;

therefore p It can be called a pointer ,p The value of is the address ;

In addition, I would like to emphasize here , The size of a block in memory is one byte , And use 16 It is convenient for human to watch ( It will be used in the second meaning of pointer )

<>2. Pointer has type 2 Meaning

introduction :
#include <stdio.h> int main() { int a = 20; int* intp = &a; char* charp = &a;
double* doublep = &a; printf("%d\n", sizeof(intp)); printf("%d\n", sizeof(charp)
); printf("%d\n", sizeof(doublep)); return 0; } /* Running results : 4 4 4 */

You're going to see it all 4, Why ? because sizeof() It measures the size of the pointer , In fact 32 On the bit platform , What is the size of the pointer 4 byte , stay 64 On the table , What is the size of the pointer 8 byte **, The pointer size has nothing to do with the pointer type , It's all the same **, Because the pointer is the address , The address is 4 Bytes or 8 Bytes ;

ask : Since the pointer size is the same ? Why so many pointer types ? Here are some examples

<> significance 1: The pointer type determines how much permission a pointer has to dereference ( It can manipulate several bytes )
#include <stdio.h> int main() { int a = 0x44332211;
/*a What's stored is a 16 Decimal digits ,16 A number in the base represents 4 position ,2 A number represents a byte */ int b = 0x44332211; int c =
0x44332211; int* intp = &a; char* charp = &b; short* shortp = &c; *intp = 0; *
charp= 0; *shortp = 0; printf("%#x\n", a); printf("%#x\n", b); printf("%#x\n", c
); return 0; } /* Running results : 0 0x44332200 0x44330000 */
a Become 0 , Changed four bytes

b Become 0x44332200, Changed a byte

c Become 0x44330000, Two bytes changed ;

* Because of the pointer intp Is an integer pointer , Has four byte permissions .charp Is a character pointer , Has one byte permission ,shortp Is a short integer pointer , Has two byte permissions ;
<> significance 2: The pointer type determines how far the pointer can go forward or backward ;

give an example :
#include <stdio.h> int main() { int a = 10; int* intp = &a; char* charp = &a;
double* doublep = &a; printf("%p",&a); printf("%p",intp+1); printf("%p",charp+1)
; printf("%p",doublep+1); return 0; } /* Running results : 00B6FB44 00B6FB48 00B6FB45
00B6FB4C */
You can see the integer pointer intp After adding one, the address moved back 4 Bytes ;

​ Character pointer charp After adding one, the address moves back one byte ;

​ Double precision pointer doublep After adding one, it moves back eight bytes ;

So that's what different pointer types mean , It determines the distance you can move back and forth , This is mainly used for array access , And data structure

<>3. Wild pointer

There are three reasons for the formation of wild pointer :

* The position of the pointer is unknown ;
* Pointer out of bounds when accessing array ;
* The space the pointer points to is freed ;
<> reason 1: The position of the pointer is unknown ;

We know that when we create a variable, if we do not initialize it , Then the variable is a random value

* for example ; int a; that a It's a random value ; In the same way , If the pointer is not initialized, it will be a random address ( It's dangerous , Thought you didn't know who he was going to point to )
Code examples :
#include <stdio.h> int main() { int a = 10; int* p; *p = 20;
/* It's scary , Because you don't know the pointer p What is it */ return 0; }
<> This time is the wild pointer

<> reason 2: Pointer out of bounds when accessing array ;

|----|----|----|----|----|----|----|----| ( Let's say the left is an array lattice , You can see that there are 8 A grid , The index is 0 1 2 3 4 5
6 7)

Code demonstration :
#include <stdio.h> int main() { int num[8] = {2,4,5,8,4,3,13,31}; int* p = num;
/* The array name is the first element address */ for (int i = 0;i<=8;i++) { printf("%d\n", *(p+i)); /* Using dereference pointer to access array */
} return 0; } /* Running results : 2 4 5 8 4 3 13 31 -858993460 */
We can see that , One more negative number , That's because when i =
8 When I was young ,p+i It means moving back 32 Bytes , Namely 8 Plastic surgery , But move back 7 We'll be at the end of the line in a few minutes ; When i yes 8 It's going to cross the line ; After crossing the border , Will point to a random address we don't know

<> So now p It's also a wild pointer

<> reason 3: The space the pointer points to is freed ;

Before we learn the pointer , You know when you create a function , Once the function is called , All arguments to the function are destroyed ; The created space will be released
#include <stdio.h> int* test() { int a = 20; return &a; } int main() { int* p
= test(); printf("%d", *p) return 0; }

In this way, there will be problems in writing . because test() In the parameter a It is destroyed when the function is output ,a We'll give it back to the system ; It's very likely that the execution here is still the same 20, The content of this space has not been cleared ; stay c There is this explanation in the language trap ( Sometimes it may have been removed ), In short, it is not recommended to write like this

<> So now p It's also a wild pointer

summary : How to avoid wild pointer ?

* It is better to initialize the pointer
* Watch out for the pointer
* Pointer to space is released or assigned without knowing how to initialize the pointer NULL
* Check validity before using pointer
<>4. Pointer operation

<>1. Pointer + - operation ( Some of them have been demonstrated before )
#include <stdio.h> int main() { int num[15] = {
21,2,1,4,5,3,6,9,5,4,7,8,2,1,12 }; int i = 0; int* p = num; for (i = 0; i < 15;
i += 2) { printf("%d ", *(p + i)); /* because p Is an integer pointer , So add 2 It's the jump 8 Bytes */ } return 0; } /*
Running results : 21 1 5 6 5 7 2 12 */
<>2. Pointer minus pointer ( In the same space )
#include <stdio.h> int main() { int arr[10] = {12,30,14,12,2,3,4,32,12,16};
printf("%d", &arr[9] - &arr[2]); return 0; } /* Running results : 7 */

So the pointer minus the pointer is the number of elements between the pointers

<> Make a question : Write your own functions , Three ways to measure the length of string , Don't use it strlen();

The first method is counting The second method is function recursion , But it also uses pointers to add and subtract integers The third method is pointer subtraction

<>3. Pointer relation operation

Operation by size comparison
#include main() { float values[5]; float* p = NULL; for(p =
&values[5];p>&values[0]) { *--p = 0; } return 0; }

When the pointer p be equal to &values[5] It's time to cross the line , But not afraid , Because we're just comparing addresses , It's not worth it

So according to the meaning of the code can be all initialized to 0 It's over

It's also written like this
#include main() { float values[5]; float* p = NULL; for(p =
&values[5];p>&values[0];p--) { *p = 0; } return 0; }
But this is not recommended , Because the final termination condition is **,p The address of is less than &values[0]**, I ran to the front of the array

and C Language standard :

Allows a pointer to an array element to be compared with a pointer to the memory location after the last element of the array , However, it is not allowed to compare with a pointer to the memory location before the first element .

<>5. Pointer and array

Generally speaking, the array name is the first element address , But there are exceptions ;

*
&arr_name & + The array name form represents the entire array , It's just when it's displayed , The address of the first element is displayed , You can tell by the code

* #include <stdio.h> int main() { int arr[10] = {0}; printf("%p", arr); printf
("%p", arr+1); printf("%p", &arr); printf("%p", &arr+1); return 0; } 003AFE90
003AFE94 003AFE90 003AFEB8
* Running results :
* 003AFE90
003AFE94
003AFE90
003AFEB8

You can see that &arr+1 The address has been increased 40 Bytes , That is, the size of the array

* sizeof(arr_name) It represents the entire array ; #include <stdio.h> int main() { int num[20] =
{0}; printf("%d", sizeof(num)); return 0; } /* Running results : 80 */
<>6. Secondary pointer

The address of the variable is stored with the pointer , And so on , It is the secondary pointer that stores the pointer address

So what is the secondary pointer representation ?

It's two * There are three more in the back * , Four * … And so on
#include <stdio.h> int main() { int a = 20; int* p = &a; int** pp = &p; return
0; }
<>7. Pointer array

Notice that I'm talking about pointer arrays , It's an array , Is an array of pointers
#include <stdio.h> int main() { int a = 20,b = 30,c = 10,d = 40; int* num[4] =
{&a,&b,&c,&d}; for(int i = 0;i<4;i++) { printf("%d\n",*num[i]); } return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?