[C language] Elementary pointers

1. What is a pointer?

Pointer is a variable type used to store the address of a variable. It can point to any data type, including basic data types (such as integers, characters, etc.) and composite data types (such as arrays, structures, etc.). Through pointers, we can indirectly access or modify data stored at a specific memory address.

  1. A pointer is the number of a smallest unit in memory, that is, the address.
  2. The pointer mentioned in normal spoken language usually refers to a pointer variable, which is a variable used to store a memory address.

Summary: Pointers are addresses. Pointers in spoken language usually refer to pointer variables.
Pointer variables are variables used to store addresses.
The question here is:

  • How big is a small unit? (1 byte)
  • How to address?

After careful calculation and weighing, we found that it is more appropriate to give one byte to one corresponding address.
For a 32-bit machine, assuming there are 32 address lines, then assuming that each address line generates a high level (high voltage) and a low level (low voltage) during addressing is
(1 or 0);
then 32 The address generated by the address line will be:

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001

11111111 11111111 11111111 11111111

There are 2 to the power of 32 addresses here.
Each address identifies a byte, then we can give (2^32Byte == 2^32/1024KB == 2^32/1024/1024MB == 2^32/1024/1024/1024GB == 4GB) 4G Space is addressed. In the same way, the 64-bit machine will have 264 addresses.
Here we understand:

  • On a 32-bit machine, the address is a binary sequence composed of 32 0s or 1s, and the address must be stored in 4 bytes of space, so the size of a pointer variable should be 4 bytes.
  • If on a 64-bit machine, if there are 64 address lines, the size of a pointer variable is 8 bytes to store one address.

Summary:
Pointer variables are used to store addresses, and addresses uniquely identify a memory unit.

2. Pointers and pointer types

We all know that variables have different types, integer, floating point, etc. Does the pointer have a type? Let's look at the code below.

int num = 10;
p = #

To save &num (the address of num) into p, we know that p is a pointer variable, so what is its type? We give the pointer variable the corresponding type.

char *pa = NULL;
int *pb = NULL;
short *pc = NULL;
long *pd = NULL;
float *pe = NULL;
double *pf = NULL;

It is not difficult to see here that
the char* type pointer is used to store the address of the char type variable.
The short* type pointer is used to store the address of the short type variable.
The pointer of type int* is used to store the address of the variable of type int.
The long* type pointer is used to store the address of the long type variable.
The float* type pointer is used to store the address of the float type variable.
The double* type pointer is used to store the address of the double type variable.
Insert image description here
Insert image description here
From these two pictures, we can see that the size of the pointer has nothing to do with the pointer type and is only related to the number of bits in the machine. On a 32-bit machine, the pointer size occupies 4 bytes, and on a 64-bit machine, it occupies 8 bytes.
What is the meaning of pointer type? Let's look down.
Insert image description here
We can see that the type of pointer determines how far (distance) the pointer moves forward or backward. pi is of type int so pi+1 is moved by 4 bytes and pc is of type char so pc is only moved by one byte.

3. Wild pointer

A wild pointer means that the location pointed to by the pointer is unknown (random, incorrect, and without clear restrictions)Causes of wild pointers:
1.The pointer is not initialized

#include <stdio.h>
int main()
{
    
    
	int *p;//局部变量指针未初始化,默认为随机值
	*p = 10;
	return 0;
}

2. Pointer out-of-bounds access

#include <stdio.h>
int main()
{
    
    
	int arr[10] = {
    
    0};
	int *p = arr;
	int i = 0;
	for(i=0; i<=11; i++)
	{
    
    
		*(p++) = i;//当指针指向的范围超出数组arr的范围时,p就是野指针
	}
	return 0;
}

3. Release the space pointed to by the pointer

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

}
int main()
{
    
    
	int* p = test();
	printf("%d\n", *p);
	return 0;
}

How to avoid wild pointers

  1. Pointer initialization (if you don’t know what value the pointer is initialized to, you can initialize it to NULL)
  2. Be careful the pointer goes out of bounds
  3. The pointer pointing to the space is released and set to NULL in time.
  4. Avoid returning the address of local variables
  5. Check validity of pointers before using

4. Pointer arithmetic

  • Adding or subtracting an integer to a pointer moves the address backward or forward.
    Insert image description here
  • The absolute value of a pointer minus a pointer is the number of elements between the pointer and the pointer,The premise of pointer minus pointer is that the two pointers point to the same space.
    Insert image description here

5. Pointers and arrays

Insert image description here
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.
Since the array name can be stored in a pointer as an address, it becomes possible for us to use a pointer to access one.
Insert image description here
So p+i actually calculates the address of the array arr with index i.
Then we can access the array directly through the pointer.
For example:
Insert image description here

6. Secondary pointer

Pointer variables are also variables. Variables have addresses. So where is the address of the pointer variable stored?
The answer is secondary pointers.
Insert image description here
The operations on secondary pointers are:

  • *ppa dereferences the address in ppa, so what is found is pa. *ppa actually accesses pa.

int b = 20;
*ppa = &b;//Equivalent to pa = &b;

  • **ppa first finds pa through *ppa, and then dereferences pa: *pa, then it finds a.

**ppa = 30;
//Equivalent to *pa = 30;
//Equivalent to a = 30;

7. Pointer array

Is an array of pointers a pointer or an array?
Answer: It's an array. is an array that stores pointers.
Arrays We already know integer arrays and character arrays.

int arr1[5];
char arr2[6];

Insert image description here
What does an array of pointers look like?

int* arr3[5];//是什么?

arr3 is an array with five elements, each element is an integer pointer.
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_58032742/article/details/131965616
Recommended