[C language] elementary pointers, super detailed, including code examples

add link description


foreword

Hello friends, the author of this article summarizes the basic knowledge of pointers he has learned in super detail. Pointer is a very important knowledge point in the process of learning C language. It is also the essence of C language. Let us learn more about pointers together! !


1. What exactly is a pointer?

  1. A pointer is the number of the smallest unit in memory, that is, the address

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

  3. Summary: A pointer is an address, and a pointer in spoken language usually refers to a pointer variable

  4. 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, so the size of a pointer variable should be 4 bytes.
    Then if on a 64-bit machine, if there are 64 address lines, the size of a pointer variable is 8 bytes to store an address

5. Summary:
Pointers are used to store addresses, and addresses uniquely identify an address space.
The pointer size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.

2. Various types of pointer information

1. Pointers and pointer types

We all know that variables have different types, integer, floating point, etc. Does the pointer have a type?
To be precise: yes.
Please see 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?
Now we need to give the pointer variable the corresponding type.

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

Here we can know:
The pointer of char* type is to store the address of 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 variable of type int.
And so on!


2. pointer ± integer

Pointers can also perform various operations, please see:

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

Summary: The type of pointer determines how much (distance) the pointer takes one step forward or backward.

3. Dereferencing pointers

After we save the address of the variable with a pointer, we need to use the dereference '*' operator to operate on the variable corresponding to the saved address. Please see the
following code:

#include <stdio.h>
int main()
{
    
    
 int n = 0x11223344;
 char *pc = (char *)&n;
 int *pi = &n;
 *pc = 0;   //Pi是char*类型的指针变量,所以它可以操作1个字节的内存空间
 *pi = 0;   //Pi是int*类型的指针变量,所以它可以操作4个字节的内存空间
 return 0;
}

Summary:
The type of pointer determines how much authority (how many bytes can be manipulated) when dereferencing the pointer.
For example: dereferencing a char* pointer can only access one byte, while dereferencing an int* pointer can access four bytes.

4. Wild Pointer

A wild pointer means that the location pointed by the pointer is unknown (random, incorrect, and without clear restrictions).
We cannot operate on the wild pointer, because the right to use the space it points to is not in our hands, and the forced operation will report an error or Harmful to the computer!

Causes of wild pointers:

  1. The pointer is uninitialized, and the local pointer variable is uninitialized to be a random value
  2. Pointer out-of-bounds access, this situation mainly occurs in arrays (when the range pointed to by the pointer exceeds the range of the array arr, p is a wild pointer)
  3. The space pointed to by the pointer is released, and the pointed space has been released, but if we forcefully operate on the address, it will cause a wild pointer exception

How to avoid wild pointers:
4. Pointer initialization
5. Beware of pointer out of bounds
6. Pointer pointer space release even if NULL
7. Avoid returning the address of local variables
8. Check the validity of the pointer before use

5. Pointer operation pointer-pointer

We must be clear: the result of subtracting pointers from pointers is: the number of elements (spaces) that differ between the spaces pointed to by these two pointers (take the absolute value if the subtraction is negative), we must be clear about this. For example, please see the
following the code:

int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int len = &arr[9] - &arr[0];
	printf("%d\n", len);
	return 0;
}

The result of the operation is as follows:
insert image description here
the address of the 10th element minus the address of the first element is the number of elements that differ between them, which is 9! ! !

6. The comparison relationship between pointers and pointers

The standard stipulates that
a pointer to an array element is allowed to be compared with a pointer to the memory location after the last element of the array, but is not allowed to be compared with a pointer to the memory location before the first element

7. Secondary pointer

A pointer variable is also a variable, and a variable has an address. Where is the address of the pointer variable stored?
This is the secondary pointer
example:

int main()
{
    
    
	int a=10;//普通整型变量
	int* p = &a;//一级整型指针
	int** pp = &p;//二级整型指针
	printf("%d\n", *(*pp));//10
	return 0;

}

Similarly, there are three-level, four-level pointers, etc.

Summarize

These are the basic pointers shared and summarized today. The blogger will update a detailed article about advanced pointers later, and will conduct a more in-depth analysis and understanding of pointers. If you like it, you can pay attention to the follow-up. Thank you for watching! ! !

Guess you like

Origin blog.csdn.net/m0_71214261/article/details/132164962