[C Language] Detailed Explanation of Pointers (1)

Pointer is an important concept in C language and an important feature of C language. Using it correctly and flexibly can make the program concise, compact and efficient. Everyone who learns and uses C language should have a deep understanding of it. Learn and master pointers.

What are pointers?

What are pointers?

  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

Summary:
A pointer is an address.

pointer variable

You can use & (address operator) to get the memory start address of the variable, and store the address in the variable. This variable is a pointer variable.

#include<stdio.h>
int main()
{
    
    
	//在内存中开辟一片空间
	int a = 10;
	//用p来接收a的地址
	int* p = &a;
	*p = 20;
	printf("%d", a);
	return 0;
}

image.png
Calculate the size of a pointer variable:

#include <stdio.h>

int main()
{
    
    
	int a = 10;
	int* p = &a;
	//计算指针的大小
	printf("%d", sizeof(p));
	return 0;
}

image.png
Summarize:

  • The value of the variable a can be changed by assigning a new value to *p.
  • Pointer variables are used to store addresses, which uniquely identify a memory unit.
  • The pointer size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms.

Pointers and Pointer Types

How pointers are defined

type +*
For example, define a pointer of type int
int * a;

type of pointer

The following are the types of pointers I listed

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

Different types of pointers are used to store addresses of variables of different types.

pointer ± integer

#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;
}

image.png
It can be seen that pc+1 has moved 1 byte backward, and pi+1 has moved 4 byte backward. These backward walking distances are all related to the type of pointer.
Summary:
The type of pointer determines how much (distance) the pointer takes one step forward or backward.

wild pointer

A wild pointer is where the pointer points to is unknowable (random, incorrect, without clear constraints).

Causes of wild pointers

  1. pointer is not initialized
  2. pointer out of bounds access
  3. The space pointed to by the pointer is freed

pointer is not initialized

code:

#include<stdio.h>

int main()
{
    
    
	int* p;
	*p = 10;//指针没有初始化,导致程序出了问题
	return 0;
}

image.png
Here the compiler directly reported the cause of the error to us. The local variable is not initialized. This is one of the cases of wild pointers, and the pointer is not initialized.

pointer out of bounds access

code show as below:

#include<stdio.h>

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

image.png
When i=sz, it exceeds the arr array and becomes a wild pointer, that is, the printed number is uncontrollable.

The space pointed to by the pointer is freed

code show as below:

int* t()
{
    
    
	int a = 10;
	return &a;
}
int main()
{
    
    
	int* p = t();
	*p = 20;
	return 0;
}

The address of the variable a is only valid in the t() function. When the address of a is passed to the pointer p, because the t function is released, the space address of the variable a is released, causing p to become a wild pointer.

How to avoid wild pointers

  1. pointer initialization
  2. Careful pointer out of bounds
  3. The pointer points to the space to release, and set NULL in time
  4. Avoid returning the address of a local variable
  5. Check the validity of the pointer before using it

pointer arithmetic

Pointer ± integer operation

#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;
}

image.png

pointer-pointer

code:

#include<stdio.h>

int main()
{
    
    
	int arr[5] = {
    
     1,2,3,4,5 };
	printf("%d ", &arr[5] - &arr[0]);//得到的是中间的元素个数
	return 0;
}

image.png

Guess you like

Origin blog.csdn.net/weixin_63284756/article/details/130737509