[C language] those who don't understand pointers must come in and take you to the initial pointer. It's not as difficult as you think.

catalogue

preface

This blog is mainly about the initial pointer, the simple use of pointer, and the use of pointer in solving problems in daily use.

I What is the pointer?

II Pointer and pointer type

2.1} pointer dereference

2.2 pointer + - integer

III Field pointer

3.1 cause of formation

1. Pointer not initialized

2. Pointer cross-border access

 3. The space pointed to by the pointer is released

3.2 how to avoid wild pointer

IV Pointer operation

4.1 pointer + - integer

4.2 pointer - pointer

Example 2: find the length of the string

4.3 pointer relation operation

V Pointers and arrays

Vi Secondary pointer

VII Pointer array

summary

preface

This blog is mainly about the initial pointer, the simple use of pointer, and the use of pointer in solving problems in daily use.

Tip: the following is the main content of this article

I What is the pointer?

Pointer: it is the number of memory unit, that is, pointer.

Pointer understood 2 Three key points:
1. A pointer is the number, or address, of the smallest cell in memory
2. The pointer in spoken English usually refers to the pointer variable, which is used to store the memory address
Conclusion: pointer is the address. In colloquial language, pointer usually refers to pointer variable.
So we can understand it as: memory
Memory unit - number - Address - pointer
The pointer in spoken English is a pointer variable, which is used to store memory variables
 
Memory is divided into small memory units. The size of a basic memory unit is one byte
Address: memory unit number 0X00000000
Generation of memory unit number: 32-bit machine - 32 address line - Physical wire - power on - 1 / 0
00000000 00000000 00000000 00000000 -- 4 bytes
                   00000000 00000000 00000000 00000001
                   00000000 00000000 00000000 00000010
                   ....     ...      ...      ...
                   11111111  11111111 11111111 11111111
                   2*10^32 bite   -- 4GB
64 bit
Pointer variable
We can pass & (take address operator) take out the actual memory address of the variable, and store the address in a variable
A variable is a pointer variable
int main()
{
	int a = 10;// Four bytes

	int* pa = &a;//pa stores an address, so pa is a pointer variable
	printf("%d\n", sizeof(pa));
	printf("%p", &a);
	return 0;
}

The following picture is easy to understand:

give an example:

int main()
{
	int a = 0x11223344;
	int* pa = &a;
	*pa = 0;
	//char* pc = &a;
	//*pc = 0;
	//int*   --> 4
	//char*   --> 1
	//double*   --> 8

	return 0;
}

Printing of address:

II Pointer and pointer type

Here we are discussing the type of pointer
As we all know, variables have different types, integer, floating point, etc. Does the pointer have a type?
To be exact: Yes.
int main()
{
	int a = 10;
	int* pa = &a;//Shaping -- 4 
 	char* pc = &a;//Character -- 1
	printf("%p\n", pa);
	printf("%p\n", pa+1);

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

	//The pointer type determines the distance the pointer takes one step forward or backward -- in bytes
	return 0;
}

The pointer type determines the distance the pointer takes one step forward or backward -- in bytes

Pointers are defined as follows: type + * .
Actually:
char* Type pointer is used to store char Address of the type variable.
short* Type pointer is used to store short Address of the type variable.
Pointer of type int * is used to store int Address of the type variable.

2.1} pointer dereference

 

Summary:
The type of pointer determines how much permission (several bytes can be operated) you have when dereferencing the pointer.
For example: char* Pointer dereference can only access one byte, and int* The dereference of the pointer can access four bytes.

2.2 pointer + - integer

Summary: the type of pointer determines how far the pointer moves forward or backward

give an example:

 

analysis:

int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		//Initializing
		*(p+i) = i+1;
	}
	//Printing
	int* m = &arr[0];
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *m);
		m++;
	}
	printf("\n");
	//Print backwards 9 8 7
	int* q = &arr[9];
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *q);
		q--;
	}
	return 0;
}

Demo analysis:

 

III Field pointer

Concept: a wild pointer means that the position pointed to by the pointer is unknown (random, incorrect, and without explicit restrictions)

3.1 cause of formation

1. Pointer not initialized

int main()
{
	int* p;//Local variable pointer is uninitialized and defaults to random value
	*p = 20;
	return 0;
}

2. Pointer cross-border access

	int arr[10] = { 0 };
	int* p = arr;
	for (int i = 0; i <= 10; i++)
	{
		*p = i;
		p++;
	}

 3. The space pointed to by the pointer is released

//The space pointed to by the pointer is released

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

int main()
{
	int a = 10;
	int* pa = &a;// Know who to deposit it with

	int* p = NULL;//Initialize to null pointer
	if (p != NULL)
	{

	}

	return 0;
}

analysis:

 

3.2 how to avoid wild pointer

IV Pointer operation

4.1 pointer + - integer

Initialize and assign print
int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		*(p + i) = i+1;
	}
	//Print
	for (i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));
	}
	return 0;
}

4.2 pointer - pointer

int main()
{
	/*int a[10] = { 0 };
	
	printf("%d\n", &a[9] - &a[0]);
	printf("%d\n", &a[0] - &a[9]);*/

	int a = 10;
	char c = 'w';


	return 0;
}

analysis:

Example 2: find the length of the string

Previously, we used the strlen function to find the length of the string

//Function strlen for string length
#include <string.h>

int My_strlen(char* s)
{
	int count = 0;
	while(*s != '\0')
	{
		count++;
		s++;
	}
	return count;

}
int main()
{
	char arr[] = "abc";
	int len =My_strlen(arr);

	printf("%d\n", len);
	
	return 0;
}

Similarly, we can also use the pointer minus pointer method

int My_strlen(char* s)
{

	char* start = s;
	while (*s != '\0')
	{
		s++;
	}
	return  s - start;
}
int main()
{
	char arr[] = "abc";
	int len =My_strlen(arr);

	printf("%d\n", len);
	
	return 0;
}

analysis:

4.3 pointer relation operation

int main()
{
	float a[5];
	float* p;
	for (p = &a[5]; p >= &a[0];)
	{
		*--p = 0;
	}
	return 0;
}

 

improvement:

	for (p = &a[4]; p >= &a[0]; p--)
	{
		*p = 0;
	}

 

Standard provisions:
A pointer to an array element is allowed to compare with a pointer to the memory location after the last element of the array, but not with
A pointer to the memory location before the first element is compared.

 

V Pointers and arrays

 Pointer -- address
 array -- A set of data of the same type
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//arr first element address
	int* p = arr;
	int i = 0;
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%p == %p \n", p + i,&arr[i]);
	}


	return 0;
}

analysis:

 

It can be seen that the array name and the address of the first element of the array are the same.
Conclusion: The array name represents the address of the first element of the array .
Then it is feasible to write code like this:
int arr [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 };
int * p = arr ; //p The address of the first element of the array is stored
Since the array name can be stored as an address in a pointer, it is possible for us to use the pointer to access one.
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int* p = arr; //Pointer to the address of the first element of the array
	int sz = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("&arr[%d] = %p   <====> p+%d = %p\n", i, &arr[i], i, p + i);
	}
	return 0;
}

So p+i actually calculates the address of array arr subscript i

Vi Secondary pointer

Pointer variable is also a variable. If it is a variable, it has an address. Where is the address of pointer variable stored?
This is it. Secondary pointer .
int main()
{
	int a = 10;
	int* pa = &a;
	int** ppa = &pa; //ppa is a secondary pointer
	**ppa = 20;
	printf("%d\n",a);

	return 0;
}

VII Pointer array

Is a pointer array a pointer or an array?
Answer: array. Is an array of pointers.
Array, we already know integer array, character array
int main()
{
	int arr[10];//Shaping array
	char ch[5];//Character array

	//Pointer array --- an array of pointers
	int a = 10;
	int b = 20;
	int c = 30;
	int* arr2[5] = {&a,&b,&c};//An array of integer pointers
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		printf("%d ", *(arr2[i]));
	}
	return 0;
}

 

summary

This article roughly summarizes the simple use of the pointer in daily learning. If it is helpful to you after reading, I hope you will like Collection + attention. Thank you for your support. If there are mistakes in the article, please correct them in time.

Keywords: C Machine Learning

Added by dmccabe on Wed, 29 Dec 2021 09:25:59 +0200