当前位置:网站首页>9. Pointer (upper)

9. Pointer (upper)

2022-07-06 13:26:00 It's Wang Jiujiu

Catalog

1. What is the pointer ?

2. The size of the pointer

3. The type of pointer

Pointer dereference (*)

The pointer +- Integers  

4. Wild pointer

The cause of the wild pointer

Avoid wild pointer

5. The operation of the pointer

The operational relationship of pointers

The pointer - The pointer

6. Pointers and arrays

7. The secondary pointer

8. Pointer array


1. What is the pointer ?

The pointer ? What is a pointer ? fundamentally , The pointer (pointer) Is a variable whose value is the memory address ( Or data objects ). just as char The value of a type variable is a character ,int The value of a type variable is an integer , The value of the pointer variable is the address . Pointer in spoken language usually refers to pointer variable , The variable used to hold the address .

int main()
{
	int num = 10;
	int* p = #
	printf("%p\n", p);
	return 0;
}
  • In the above code ,& To get the address symbol , Put the variable num Take out the address of and assign it to p.
  • * Indicates that the declared variable is a pointer , and int Indicates that the type of pointer is integer ;int*p It means ,p It's a pointer ,*p yes int type .
  • utilize printf When the function prints the address ,%p The corresponding address is .

With X86 Run the above code in the environment , give the result as follows :

009BFA40 In order to 16 In base notation , Variable num It is stored in this address .

Just like the address in our life ,C The address in the language is also unique .

2. The size of the pointer

Since the pointer is used as a variable to store the address , Then the computer will certainly open up a space for storing pointers .

Take the above code as an example , adopt sizeof You can calculate the size of the pointer :

int main()
{
	int num = 10;
	int* p = #
	printf("%d\n", sizeof(p));
	return 0;
}

stay X86 In the environment , namely 32 In the machine : Pointer 4 Bytes .

stay X64 In the environment , namely 64 In the machine : Pointer 8 Bytes .

1 Bytes are equal to 8 A bit , that 32 In the machine , The pointer can indicate 2^32 An address ;64 In a bit machine , Can represent 2^64 An address .

The smallest unit in the pointer is a byte , This is after careful thinking and weighing . That is, we can query a char Address of type character , But you can't query the address of a bit in this character .

3. The type of pointer

When creating a variable , We need to declare the type for it , for example : Use integer to express age ,float Type indicates height 、 Weight etc. ; The same is true when creating pointers , For different types of variables , Declare a pointer of the corresponding type to store its address .

char  *pc = NULL;
int   *pi = NULL;
short *ps = NULL;
long  *pl = NULL;
float *pf = NULL;
double *pd = NULL;

(* And pointer name , Not affecting use )

Pointer dereference (*)

Through the indirect operator *(indirection operator) You can use the address to change the variable , This operation is also called “ Quoting ”.

#include<stdio.h>
int main()
{
	int num = 10;
	printf(" Before change :%d\n", num);
	int* p = &num;
	*p = 2;
	printf(" After change :%d\n", num);
	return 0;
}

  We have not changed directly num Value , It's about finding num Storage address , Through dereference operation, it indirectly changes num Value .

The pointer +- Integers  

#include<stdio.h>
int main()
{
	int num = 10;
	int* pi = &num;
	char* pc = (char*)&num;

	printf("%p\n", &num);
	printf("%p\n", pi);
	printf("%p\n", pi + 1);
	printf("%p\n", pc);
	printf("%p\n", pc + 1);

	return 0;
}

expression char* pc = (char*)&num: Will integer variable num Take out your address , Cast to char* type , Then give to char* Pointer variable of type pc.

Found by test : The pointer type corresponds to the pointer +- Time span , namely char Pointer to type +1 Visit later 1 Bytes ,int Pointer to type +1 Visit later 4 Bytes .

4. Wild pointer

Concept : The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )

The cause of the wild pointer

Pointer not initialized

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

Local variable pointer not initialized , The default value is random .

Pointer cross boundary access

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i < 11; i++)
	{
		*(p++) = i;
	}
	return 0;
}

Array arr The subscript of is 0~9, When the pointer points to an array arr The scope of time ,p It's a wild pointer .

The space that the pointer points to is released

When the variable pointed to by the pointer is released , The pointer is a wild pointer .

Avoid wild pointer

1. Pointer initialization

2. Watch out for the pointer

3. Pointer to space release even if set to NULL

4. Avoid returning the address of a local variable

5. Check the validity of the pointer before using it (avoid)

#include <stdio.h>
#include <assert.h>
int main()
{
	int* p = NULL;
	int a=10;
	int* p = &a;
	assert(p != NULL);
	*p = 10;
	if(p!=NULL)
	*p = 20;
	return 0;
}
assert Is an assertion function , Need to include header file <assert.h>, adopt assert and if Functions can avoid wild pointers .

5. The operation of the pointer

The operational relationship of pointers

#include<stdio.h>
#define N 5
int main()
{
    int ch[N] = { 0 };
    int* vp;
	for (vp = &ch[0]; vp < &ch[N];)
	{
		*vp++ = 1;
	}
	return 0;
}
  • expression vp = &ch[0] Indicates that the address of the first element in the array is assigned to vp.
  • vp < &ch[N] It means that you will vp The address stored in the array is compared with the address of the next element of the array , as long as vp Less than it , It means that it is still accessed in the array , No boundaries .
  • *vp++ It's actually *(vp++), One integer per backward access .

The pointer - The pointer

#include<stdio.h>

int my_strlen(char* s) 
{
	char* p = s;
	while (*p != '\0')
		p++;
	return p - s;
}

int main()
{
	char ch[]="hello";
	printf("%d\n", my_strlen(ch));
	return 0;
}
Through the pointer, you can simulate the realization of finding the length of the string , The pointer - The pointer is the number of elements between these two addresses .

The standard stipulates

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 .

6. Pointers and arrays

This part is dedicated to a chapter , Please have a look at Array 、 The relationship between pointer and array .

7. The secondary pointer

Pointer variables are also variables , If it is a variable, it must have a storage address . Then the pointer that stores the address of the pointer variable , It is called secondary pointer .

#include<stdio.h>

int main()
{
	int a = 10;
	int* p1 = &a;
	int** p2 = &p1;
	**p2 = 20;
	return 0;
}
The pointer p1 Store variables a The address of , Secondary finger p2 Store the first level pointer p1 The address of , Through to p2 Quoting (*p2) find p1, Then dereference it (**p2) Find the variable a.

8. Pointer array

Pointer arrays are arrays , It's just an array for storing pointers .

#include<stdio.h>

int main()
{
	int arr1[] = {1,2,3};
	int arr2[] = {4,5,6};
	int arr3[] = {7,8,9};
	int* p1 = arr1;
	int* p2 = arr2;
	int* p3 = arr3;
	int* p[] = { p1,p2,p3 };
	return 0;
}

int* p[] = { p1,p2,p3 } Indicates that the type of data stored in the array is int* type , The stored data are p1,p2,p3.

int i = 0, j = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			printf("%d ", *(*(p + i))+j);
		}
		putchar('\n');
	}

  Set up two for Loop print pointer array p, It can be found that one-dimensional pointer array realizes the effect of two-dimensional array .

原网站

版权声明
本文为[It's Wang Jiujiu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916176145.html