当前位置:网站首页>[C language] summary of basic knowledge points of pointer

[C language] summary of basic knowledge points of pointer

2022-06-12 01:35:00 MuShan-bit

The pointer ( One ) Basics

One The concept of pointer

  1. To facilitate access to the contents of memory , Give each memory unit a number , We call this number address , And are pointers .
  2. A pointer is also a data type So pointers have their own memory What's stored is the address ( Number )

The four elements of the pointer

  1. The type of the pointer itself Remove the pointer name , The rest is the type of the pointer itself
  2. The type pointed to by the pointer Remove the pointer name and a *, The rest is the type pointed to by the pointer
  3. The memory of the pointer itself Used to store a number (4 byte )
  4. The memory that the pointer points to It can be all kinds of
    int num = 0;          //  type : int
    int arr[10] = {
    };     //  type : int [10]
    int* MuShan = {
    1,2,3};
    // Remove the name , The rest is the type 

Two Definition of pointer

1 Operator

*: Definition time , Indicates that the definition is a pointer Other times, it means resolving references

&: Fetch address ( Used to get the address )

2 Definition

There are many ways to understand the definition of pointers ;

  1. type Variable name ;
int* p; //  type : int
        //  Variable name : *p
  1. The type of the pointer itself Pointer name ;
int* p; //  The type of the pointer itself : int*
        //  Pointer name : p
  1. The type pointed to by the pointer * Pointer name ;
int* p; //  The type pointed to by the pointer : int
        //  Pointer name : p

Back to :

  1. Remove the pointer name , The rest is the type of the pointer itself
  2. Remove the pointer name and a *, The rest is the type pointed to by the pointer
int****** p1; // p1 Type of itself : int****** 
              // p1 Type of point : int***** (6 The level pointer can point to 5 Level pointer )

3、 ... and Pointer memory

  1. All pointers , Whatever the type , Occupy... In memory 4 Bytes of memory ( It's the address )( Specific and 64/32 Bit environment related )
#include <stdio.h>
int main()
{
    
	char*       pch;
	short*      psh;
	int*        pn;
	float*      pf;
	double*     pd;
    
	printf("%d\n", sizeof(pch));  // 4
	printf("%d\n", sizeof(psh));  // 4
	printf("%d\n", sizeof(pn));   // 4
	printf("%d\n", sizeof(pf));   // 4
	printf("%d\n", sizeof(pd));   // 4
    
	return 0;
}
  1. Point to the starting address
int num = 10;
int* p = &num;

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-naQJWXaU-1644340436740)(C:\Users\admin\Desktop\ Six Star Education \ Study \ The pointer \ The pointer ( One )]\ Pointer basis ( One )1-1.png)

Four Initialization and assignment of pointer

1 Use the address of the corresponding type variable

	int num = 10;
	int* pn = &num;   // initialization 

	float f = 3.14f;
	float* pf;
	pf = &f;          // assignment 

2 Use the same type of pointer

	int num = 10;
	int* pn = &num;     //  Initial value 
	int* p1 = pn;       //  initialization 
	int* p2;
	p2 = pn;            //  assignment 

3 Direct address

	int* p = (int*)0x36;

4 Using array names

The first level pointer can accept the array names of a stack of one bit arrays

	int arr[5] = {
     1,2,3,4,5 };
	int* p = arr;

5 character string

#include <stdio.h>
int main()
{
    
	//  The array name is the first address of the array 
	char arr[5] = {
     'a','b','c' };
	char* p = arr;
	printf("%s\n",p);     // Output :abc
	char str[5] = "abcd";
	p = str;
	printf("%s\n", str);  // Output :abcd
	printf("%s\n", p);    // Output :abcd
	// ==> char* The pointer of type can be directly used to print the whole string to '\0' stop it 
	const char* p1;
	p1 = "1234";
	printf("%s\n",p1);    // Output :1234
	const char* p2 = "Mushan";
	printf("%s\n",p2);    // Output :Mushan
	return 0;
}

6 empty

	int* p = NULL;
	int* p1 = (int*)0x0;
/* NULL: #define NULL 0 0 Address   occasionally , The pointer is defined , But it doesn't point to   Or is it , The pointer is used up , There's no point   The pointer doesn't know where to point ( Will randomly point to )  Pointer at this point , It's dangerous ( Wild pointer )  therefore   Pointers in these cases   Arrange an address for them to point to   Point to 0 Address  */

7 Multi level pointer

#include<stdio.h>

int main() {
    
	int num = 10;
	printf(" num = %d\n", num);        // num = 10;
	printf("&num = %X\n", &num);       // &num = 10FFA78
    
	int* p = &num;
	printf("*p = %d\n", *p);           // *p = 10 (num value )
	printf(" p = %X\n", p);            // p = 10FFA78 (num The address of )
	printf("&p = %X\n", &p);           // &p = 10FFA6C

	int** pp = &p; //  A secondary pointer 
	printf("**pp = %d\n", **pp);       // **pp = 10 (num value )
	printf(" *pp = %X\n", *pp);        // *pp = 10FFA78 (num The address of )
	printf(" pp = %X\n", pp);          // pp = 10FFA6C (p The address of )
	printf(" &pp = %X\n", &pp);        // &pp = 10FFA60 

	int*** ppp = &pp; //  A three-level pointer 
	printf("***ppp = %d\n", ***ppp);   // ***ppp = 10 (num value )
	printf(" **ppp = %X\n", **ppp);    // **ppp = 10FFA78 (num Address )
	printf(" *ppp = %X\n", *ppp);      // *ppp = 10FFA6C (p The address of )
	printf(" ppp = %X\n", ppp);       // ppp = 10FFA60 (pp The address of )
	printf(" &ppp = %X\n", &ppp);      // &ppp = 10FFA54

	return 0;
}

5、 ... and The addition and subtraction of pointers

The core : The value of the pointer itself ( Point to ) There is no change

Pointer offset

  1. The pointer can add or subtract an integer

  2. Pointer plus or minus an integer , It's actually offset

  3. The range of the offset is an integer unit of addition or subtraction

    Company : The number of bytes in memory of the type pointed to by the pointer

    The offset : The pointer doesn't change , But you can get the content according to the offset

#include <stdio.h>
int main()
{
    
	int num = 10;
    
	int* p = &num;
	printf("%X\n", p);          // EFFB5C
	printf("%X\n", p + 1);      // EFFB60
    
	return 0;
}

6、 ... and The self increasing and self decreasing of the pointer

Increase and decrease , Will change the pointer to

++: Indicates that the pointer moves back one unit

– : Indicates that the pointer moves forward one unit

Company : The number of bytes in memory of the type pointed to by the pointer

#include <stdio.h>
int main()
{
    
	int num = 10;
	int* p = &num;
	printf("%X\n", p);          // EFFB5C
	printf("%X\n", p + 1);      // EFFB60

	printf("%d\n",*p);          // 10
	printf("%X\n", p += 1);     // EFFB60
	printf("%d\n",*p);          // -858993460( meaningless )
	return 0;
}

7、 ... and Traverse the array through the pointer

Traversing a one-dimensional array

#include <stdio.h>
int main()
{
    
	int arr[10] = {
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	for (size_t i = 0; i < 10; i++)
	{
    
		printf("%2d", arr[i]);
	}
	printf("\n");
	int* p = arr;
	// p and arr, except arr Is outside of a constant , The others are almost the same 
	for (size_t i = 0; i < 10; i++)
	{
    
		printf("%2d", p[i]);
	}

	printf("\n");
	printf("%d\n", p[0]);        // 0
	printf("%d\n", *(p + 0));    // 0
	printf("%d\n", p[1]);        // 1( Offset first   Post value )
	printf("%d\n", *(p + 1));    // 1
	// p[n] <==> *(p+n)
	return 0;
}
// p[n]: It's called subscript form 
// *(p+n): A form called pointer offset 

Traversing a two-dimensional array

  1. Two dimensional arrays are also arrays

  2. A two-dimensional array can be regarded as a one-dimensional array whose elements are one-dimensional arrays

  3. The memory of the array is continuous

#include<stdio.h>

int main()
{
    
	int arr[3][4] = {
    
        {
    1,2,3,4},
		{
    5,6,7,8},
		{
    9,10,11,12}
	};

	for (size_t i = 0; i < 3; i++)
	{
    
		for (size_t j = 0; j < 4; j++)
		{
    
			printf("%3d", arr[i][j]);
		}
		printf("\n");
	}

	int* p0 = arr[0];
	int* p1 = arr[1];
	int* p2 = arr[2];
	printf("\n");
	// 1:
	for (size_t i = 0; i < 4; i++)
	{
    
		printf("%3d", p0[i]);           // 1 2 3 4
	}
	printf("\n");
	for (int i = -4; i <= 7; i++)
	{
    
		printf("%3d", p1[i]);           // 1 2 3 4 5 6 7 8 9 10 11 12
	}
	printf("\n");
	for (int i = 0; i < 12; i++)
	{
    
		printf("%3d", arr[0][i]);       // 1 2 3 4 5 6 7 8 9 10 11 12
	}
	printf("\n");
	//  Subscript :  Ensure that the array does not cross the boundary 
    
	// 2:
	int* p = &arr[0][0];
	for (int i = 0; i < 12; i++)
	{
    
		printf("%3d", arr[0][i]);       // 1 2 3 4 5 6 7 8 9 10 11 12 
	}
	printf("\n");
	for (int i = 0; i < 12; i++)
	{
    
		printf("%3d", *p);              // 1 2 3 4 5 6 7 8 9 10 11 12
		p++;
	}
	printf("\n");
	return 0;
}

原网站

版权声明
本文为[MuShan-bit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011339286065.html