当前位置:网站首页>Basic pointer ~ guide you to the introduction pointer

Basic pointer ~ guide you to the introduction pointer

2022-06-09 09:29:00 I usually pass by banyuanjun

What is a pointer ?

The pointer , Is the number of the smallest cell in memory , That's the address , The pointer we usually talk about orally is actually a pointer variable , That is, the variable used to store the address .

When we use sizeof To calculate the size of a pointer variable , Will find , No matter what type of pointer variable , It's the same size ,32 Bit platform yes 4,64 Bit platform is 8, This may cause people's doubts , Since the size of each pointer variable is the same , So what's the use of different types of pointers ? In fact! , The type of pointer determines how many bytes will be modified or changed during pointer calculation .

1: Pointer type determines when pointer variables are dereferenced , Number of bytes modified

such as int* It's four bytes ,char* It's one byte .

We give the following real column to make you understand it more quickly :

#include<stdio.h>
int main()
{
        int a = 0x11223344;
        char *pc = (char*)&a;
        *pc = 0;
        printf("%x",a);
       return 0;
}

We can see , Only 44 Has been changed to 00, This is because 4 Bytes stored in base 16 Base number , and a Every two digits in a byte , also pc yes char* Type of , Only one byte can be accessed , therefore a Changed to 11223300;

2: The pointer type determines the addition and subtraction of the pointer , Can skip a few bytes

such as int* Pointer variable of type , When he +1 Informed -1 Will increase 4 Bytes or less 4 Bytes ;

But the pointer has another problem , That's the wild pointer problem ;

Wild pointer  

Wild pointers are usually caused by the following problems :

1: Created a pointer variable without initializing

2: The range pointed to by the pointer variable exceeds the range of the array , Became a wild pointer

3: The address pointed to by the pointer is the address of the variable inside the function  

Only the third point is difficult to understand , For your convenience , Here is a simple list :

#include<stdio.h>
int* test()
{
	int a = 10;
	return &a;
}
int main()
{
	int* p = test();
	return 0;
}

  here p A pointer variable points to a function a The address of , But we all know that , After the function is used up, its internal variables will be destroyed , therefore p The address pointed to by the variable is gone , therefore p Became a wild pointer .

Here are some tips to avoid wild pointers :

1: When the pointer is initialized , If there is no variable to point to , It's initialized to NULL

2: The pointer can only be dereferenced if it stores a valid address

3: Avoid using a pointer to the address of a local variable

4: Use the pointer to check the validity first

  Pointer arithmetic

Pointer variables are also variables , Of course, the pointer also has its own way of operation ,

1: Pointers add and subtract integers  

The pointer plus or minus the integer will move the address size of the corresponding byte , We can access the array directly in this way .

such as :

#include<stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	for (int i = 0; i < 10; i++)
	{
		*(p + i) = 1;
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
}

 

The result is as follows. .

2: Pointer minus pointer

  The premise of subtracting pointer from pointer is that two pointers point to the same space , And the absolute value of subtracting two pointers is the number of two elements .

#include<stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p1 = arr;
	int* p2 = (arr + 10);
	printf("%d", p2 - p1);
	return 0;
}

 

 3: Pointer relation operation ( Compare the size )

The premise is that two pointers must point to the same space ;

But one thing to note is , In the standard, it is allowed to compare the pointer to the array element with the address of the last element after the last element of the array , But it is not allowed to compare with the address of an element before the first element of the array  

And arrays are closely related to pointers , The element name of an array is actually the address of the first element of the array , So you can use pointers to access arrays , As for how to access , It has been shown before and will not be shown again .

We talked about the first level pointer before , Of course, we also need a second level pointer , The concept of secondary pointer is very simple , Is the pointer that stores the first level pointer address .

Pointer variables can also be stored in arrays , An array that is all pointer variables is a pointer array ;

Pointer array

The form of pointer array is as follows

int * a[10];

a The array stores int* Variable of type . 

原网站

版权声明
本文为[I usually pass by banyuanjun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090851359707.html