Detailed explanation of the initial pointer of c language, suitable for beginners

Posted by PHPMagician on Wed, 22 Dec 2021 02:56:50 +0100

Knowledge points in this section:

1. What is the pointer

2. Pointer and pointer type

3. Field pointer

4. Pointer operation

5. Pointers and arrays

6. Secondary pointer

7. Pointer array

1, What is the pointer

Two key points of pointer understanding:

1. The pointer is the number of the smallest cell in memory, that is, the address

2. The pointer in spoken English usually refers to the pointer variable, which is used to store the address variable

Let's explain the pointer in detail:

Memory is a large space. For better management, we divide it into small memory units. The size of a basic memory unit is one byte. We number each memory unit, just like numbering a room. This number is called an address, so that we can quickly find the memory unit we are looking for.

The number / address / pointer of the memory unit expresses the same meaning

How to generate the extended supplementary address number:

32-bit machine -- there are 32 address lines, which are physical wires -- whether the wires are powered on or not will produce 0 / 1 results, that is, the electrical signal will be converted into digital signal, which will produce the following results:

00000000 00000000 00000000 00000000 

00000000 00000000 00000000 00000001

......

01111111 11111111 11111111 11111111

10000000 00000000 00000000 00000000 

......

11111111 11111111 11111111 11111111

These binary sequences are used as numbers

2, Pointer and pointer type

1. Pointer variable

We use & (address operator) to get the memory starting address of the variable and store the address in a variable, which is the pointer variable, that is, the pointer variable, which is used to store the address.

Size of pointer variable:
On a 32-bit machine, the address is a binary sequence composed of 32 0 / 1, so the address needs 4 bytes of space to store, so the size of a pointer variable is 4 bytes. (1 byte = 8} bit, so 32 bit=4 bytes)

Similarly, on a 64 bit machine, the size of a pointer variable is 8 bytes.

2. Pointer type

Since the pointer size is fixed, what is the meaning of defining the pointer type?

The first meaning: the pointer type determines how many bytes can be accessed at a time when dereferencing (the permission of the pointer)

Let's look at these two codes

#include <stdio.h>
int main()
{
	int a =0x11223344;
	int* pa = &a;
	*pa = 0;
	
	char* pc = &a;
	*pc = 0;
	//The pointer type determines how many bytes can be accessed at a time when dereferencing (the permission of the pointer)
	//Int * 4 bytes
	//Char * 1 byte
	//Double * 8 bytes
}

So what does this do?

For example, in a block of memory, if we want to access four bytes, we can use int* / float * pointers; If we want to access a byte, we can use a char * pointer.

The second meaning: determines how far the pointer moves forward or backward (in bytes)

#include <stdio.h>
int main()
{
	int a = 10;
	int* pa = &a;
	char* pc = &a;
	printf("%p\n", pa);
	printf("%p\n", pa+1);
	printf("%p\n", pc);
	printf("%p\n", pc+1);
	return 0;
}

This is the result of the above code:

III. field pointer

3.1 cause of formation

1. Pointer not initialized

int main()
{
	int* p;  //Local variables are uninitialized and default to random values
	*p = 20;
	return 0;
}

2. Pointer cross-border access

int main()
{
	int arr[10] = { 0 };
	int* p = arr;
	int i = 0;
	for (i = 0; i <= 10; i++) //When the range pointed by the current pointer exceeds the range of array arr, p is the wild pointer
	{
		*p = i;
        p++;
	}
	return 0;
}

3. The space pointed to by the pointer is released

int* test()
{
	int a = 100;
	return &a;
}
int main()
{
	int* p=test();
	printf("%d", *p);//When the test function is given, a is destroyed
	return 0;
}

The destruction here is not real destruction, but the use right is returned to the system. For example, p still points to the address of a, but the value of a may have changed, not necessarily 100, but the value randomly assigned by the system

3.2 how to avoid wild pointer

1. Pointer initialization: two initialization forms

int main()
{
	int a = 0;
	int* pa = &a;//Know exactly who to point to
	 
	int* p = NULL;//If you don't know who to point to, point to NULL
	return 0;
}

2. Be careful that the pointer is out of range

3. The pointer points to the space release and is set to NULL in time

4. Avoid returning the address of local variables

5. Check the validity of the pointer before use

Statement to judge validity: if(p! = NULL)

4, Pointer operation

1. Pointer + - integer

Example: print 10-1 with pointer

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

The output results are as follows:

2. Pointer - pointer

Pointer - the premise of pointer is that two pointers point to the same space

int main()
{
	int arr[10] = { 0 };
	printf("%d\n", &arr[9] - &arr[0]); //The number of elements between two addresses is subtracted from two addresses
	printf("%d\n", &arr[0] - &arr[9]);  
	return 0;
}

The output results are as follows:

 

3. Pointer relation operation (pointer comparison size)

for (vp = &valuse[N_VALUSE]; vp > &valuse[0];)
	{
		*--vp = 0;
	}

5, Pointers and arrays

Arrays and pointers are two things in themselves. The connection is that arrays can be accessed through pointers

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%p == %p\n", p + i,&arr[i]);
	}
}

The output results are as follows:

6, Second order pointer

int main()
{
	int a = 10;
	int* pa = &a;
	int** ppa = &pa;//ppa is a secondary pointer
	int*** pppa = &ppa;//pppa is a three-level pointer
    **ppa = 20;
	printf("%d\n", a);//Access a through a secondary pointer
	return 0;
}

pa stores the address of a

ppa stores the address of pa

pppa stores the address of ppa

7, Pointer array

int main()
{
	int arr[10];//Integer array -- an array that holds integers
	char ch[5];//Character array -- an array of characters
	int a = 10;
	int b = 20;
	int c = 30;
	int* arr2[5] = {&a, &b, &c};//Pointer array -- an array of pointers
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		printf("%d ", *(arr2[i]));
	}
	return 0;
}

The output results are as follows:

 

Topics: C Back-end