Pointer from entry to advanced

Posted by gandelf1000 on Mon, 14 Feb 2022 14:36:11 +0100

 

 

catalogue

1, The concept of pointer

2, Pointer and pointer type

2.1 pointer addition and subtraction integer

2.2 dereference of pointer

3, Field pointer

3.1 causes of field pointer

1. Pointer not initialized

2. Pointer cross-border access

3. The space pointed by the pointer is released

3.2 how to avoid wild pointer

4, Pointer operation

4.1 pointer + - integer

4.2 pointer - pointer

5, Pointers and arrays

6, Summary

1, The concept of pointer

As the name suggests, a pointer can point to a certain position; Pointer variable is the address used to store a number or a block of space;

#include <stdio.h>
int main()
{
	int a = 10;//Open up a space in memory
	int* pa = &a;//Here, to address the variable a, use the & operator
				 /*a The variable occupies 4 bytes. Here, the address of the first byte of the 4 bytes of a is stored in the p variable
	In, p is a pointer variable*/
	printf("%d ", *pa);
	return 0;
}

2, Pointer and pointer type

Among the data types we have learned, there are integer, short integer, floating point, long integer and so on; Do pointers have types? How are pointers defined?

Pointer definition method:

Type + * + variable name

	char* pc = NULL;//Character pointer --- the pointer variable is pc, and its type is char* 
	int* pi = NULL;//Integer pointer --- the pointer variable is pi and its type is int* 
	short* ps = NULL;//Short integer pointer --- the pointer variable is ps, and its type is short* 
	long* pl = NULL;//Long pointer --- the pointer variable is pl and its type is long* 
	float* pf = NULL;//Single precision floating-point pointer --- the pointer variable is pf and its type is float* 
	double* pd = NULL;//Double precision floating-point pointer --- the pointer variable is pd, and its type is double* 

2.1 pointer addition and subtraction integer

Since the pointer is stored in an integer or block, what happens to the addition and subtraction of the pointer?

#include <stdio.h>
int main()
{
	int n = 10;
	int* pi = &n;
    //%p --- the address is printed in hexadecimal form
	printf("%p\n", &n);
	printf("%p\n", pi);
	printf("%p\n", pi + 1);
	return 0;
}

2.2 dereference of pointer

The pointer stores the address of a number or a space. When we want to get the content of this number or space, we need to dereference; For example: you need a key to enter the dormitory at the beginning of school. You can only open the door with a key; You need an account and password to log in to wechat, QQ and Weibo. You can only see the content with an account and password; These things we need can be understood as dereference operations;

#include <stdio.h>
int main()
{
	int n = 10;
	int* pi = &n;
	printf("%d\n", *pi);
	//Here, a '*' sign is added before pi to dereference the address stored in PI in order to get the value corresponding to this address
	return 0;
}

3, Field pointer

Concept: Wild pointer means that the position pointed by the pointer is unknowable (random, incorrect and unrestricted)

3.1 causes of field pointer

1. Pointer not initialized

When we create a local variable, it is uninitialized. By default, this variable is a random value; The same is true in local variable pointers;  

#include <stdio.h>
int main()
{
int *p;//Local variable pointer is uninitialized and defaults to random value
  *p = 20;
return 0;
}

2. Pointer cross-border access

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i <= 11; i++)
	{
		//When the range pointed to by the pointer exceeds the range of array arr, p is the wild pointer
		*(p++) = i;
	}
	return 0;
}

3. The space pointed by the pointer is released

You can have a brief introduction here. I won't give you a detailed introduction first. I'll make a supplement for you later;

3.2 how to avoid wild pointer

1. The pointer needs to be initialized
2. Be careful that the pointer is out of range
3. The pointer points to the space release even if it is set to NULL
4. Avoid returning the address of local variables
5. Check the validity of the pointer before use

Here's the fourth point:

Look at the following code. Just after touching the knowledge of pointer, I made up such code. I thought that the final print out was 10. Is the result really 10?

int* test()
{
	int a = 10;
	int* p1 = &a;
	return p1;
}
int main()
{
	int* p=test();
	printf("%d\n", *p);
	return 0;
}

Through the compilation, the result is 10, which is really happy;   

Do you think it's really 10?

#include <stdio.h>

int* test()
{
	int a = 10;
	int* p1 = &a;
	return p1;
}
int main()
{
	int* p=test();
	printf("hehe\n");
	printf("%d\n", *p);
	return 0;
}

According to the above code, only one more one is printed in advance. Ha ha, in essence, it should not affect the final result; However, the results after commissioning are as follows:

The result is 5, which means avoiding returning the address of local variables

The simple is that the space is covered;

Specific reasons: (see the creation and destruction of function stack frames) https://blog.csdn.net/sjsjnsjnn/article/details/122811828?spm=1001.2014.3001.5501

4, Pointer operation

4.1 pointer + - integer

#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		
		printf("%d ", *(p + i));//The pointer of the subscript 0 ~ 9 is referenced and the corresponding value is found
	}
	return 0;

4.2 pointer - pointer

#include <stdio.h>
int my_strlen(char* s)
{
	char* pc = s;
	while (*pc != '\0')
	{
		pc++;
	}//Here is the number of elements obtained by subtracting the address of '\ 0' from its first address (pointer pointer)
	return pc - s;
}
int main()
{
	char c[]="abcdef";
	int ret = my_strlen(c);
	printf("%d\n", ret);
	return 0;
}

 

5, Pointers and arrays

Array is a continuous space. If the address of the array is stored in the pointer variable, combined with the above, what will happen?

#include <stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,0 };
	int* p = arr;
	printf("%p\n", arr);
	printf("%p\n", &arr[0]);
	printf("%p\n", p);
	return 0;
}

From the running results, the address of the array name and the address of the first element of the array are the same as the address of p storage;

Conclusion: when the array stores the address in the pointer variable, it stores the address of the first element of the array, or p stores the address of 1;

6, Summary

The above is the introduction of the pointer. Help the little partner who has just contacted the C language to recognize the initial face of the pointer and update the advanced level of the pointer immediately. The content will be very long, but very rich.

 

Topics: C++ Back-end