Basic understanding of pointers (simple, easy to understand, super detailed!!!)

Table of contents

1. Supplementary basic knowledge

(1) Computer language representation

(2) Basic computer units and data types

(3) Binary numbers

Two, the pointer

(1) What is a pointer?

(2) Pointer variable

         (3) Pointer variable size and pointer type

① pointer variable size 

② pointer type

③Print the entire array element with a pointer

 (4) Pointer arithmetic

 ① pointer + - integer

 ② pointer - pointer

(5) Secondary pointer

(6) pointer array

Three, mutual encouragement


1. Supplementary basic knowledge

(1) Computer language representation

    In modern computers, digital integrated circuits are mainly used. Digital circuits can only represent 0 and 1 through high and low levels , so it appears that computers can only recognize 0 and 1 . Whether it is storage or computing, computers are completed using a binary system.

    For example: decimal  4  ---------- is expressed as 00000 100 in binary .

(2) Basic computer units and data types

    1. bit------------ bit

    2. byte --------- byte------------- 1byte = 8bit

    3. KB ------------ kilobyte---------- 1KB = 1024byte

    4. MB ----------- trillion ---------------- 1MB = 1024KB

    5. GB ----------- gigabytes---------- 1GB = 1024MB

    6. TB ----------- Terabyte---------- 1TB = 1024GB

    7. PB ----------- petabytes ----------- 1PB = 1024TB

Among them, some specific data type storage in C language also has a certain range:

    1. char -------- 1 byte 2. short --------- 2 bytes

    3. int -------- 4 bytes 4. long ---------- 4 bytes

    5. long long -------- 8 bytes 6. float ---------- 4 bytes


(3) Binary numbers

   The binary representation of numbers in computers is also called machine numbers. Binary numbers have two characteristics:

   1. Binary numbers are signed, that is, positive and negative numbers. Among them, if it is a positive number, the highest digit displays 0, and if it is a negative number, the highest digit displays 1

    2. Binary bits are usually limited by the data type, for example, char type has 8 bits per byte.

    Example: int 2 ; ------------------ Binary representation: 00000000 00000000 00000000 00000010


Two, the pointer

(1) What is a pointer?

    When you are new to pointers, or reading pointers, you will definitely find pointers boring. This time I will show you how to learn pointers from another angle that I understand.

    First of all, if you want to understand pointers, you must know what is memory, memory: it is the space for storing things, and drawing pictures are used to explain memory more vividly.

The picture on the left is the memory . There are 9 spaces in this memory, and one space represents one byte.

The picture on the right is to create two variables int a and a char ch in this memory space. Based on basic knowledge, we can know that int occupies 4 bytes and char occupies 1 byte . Pay attention to each small space in the middle of the memory (that is, 1 byte) is numbered (1, 2, 3, 4, 5, 6, 7, 8, 9) , the number of the memory is the address, and the address is also called a pointer.

Summary: 1. The number of the memory is the address, and the address is also called a pointer.

           2. The pointer is the number of the smallest unit in memory, that is, the address.

           3. Number -------->Address---------->Pointer

(2) Pointer variable

    A pointer variable is a variable used to store a memory address. Code demo: 

#include <stdio.h>
int main()
{
	int a = 10;  //创建一个变量 a = 10
	int* pa = &a;  // 将 a  的首地址传给指针变量 int *pa 
	// 注意:* 为解引用符   用来找到此pa地址里存放的数值   pa 存放 a 的地址
	// 将 * 和 pa 结和在一起就是指针变量  int 为指针变量类型
	// 指针变量只存放 a 的首地址   用变量类型来判断 a 的完整地址
	// 此时 int a 为4个字节   int *pa 会先找到 a 的首地址,根据 int 在向下找3个字节,就是变量 a   
	printf("%d\n", *pa);  // 10
	*pa = 20;
	printf("%d\n", a);    // 20
	return 0;
}

 Example: int a=10; int *pa = &a;

Note: 1.: * is the dereference character used to find the value stored in the address of this pa, the address where a is stored in pa.

           2: The combination of * and pa is the pointer variable int as the pointer variable type.

           3: The pointer variable only stores the first address of a Use the variable type to judge the complete address of a.

           4: At this time, int a is 4 bytes. int *pa will first find the first address of a, and find 3 bytes downwards according to int, which is the variable a.

           5: pa also has its own address, which stores other people's addresses.

(3) Pointer variable size and pointer type

    The type of the pointer has a certain meaning. In int *pa, it is not just to apply for 4 bytes, but it represents a profound meaning. It cannot be added casually, such as int, char, short, etc. The following will show you in the form of code why to set the type of the pointer.

① pointer variable size 

1. On a 32-bit machine, a pointer variable can store an address of 4 bytes.
2. On a 64-bit machine, a pointer variable can store an address of 8 bytes.

// 在32位的机器上
#include <stdio.h>
int main()
{
	int* pa;
	char* pc;
	float* pf;
	printf("%d\n", sizeof(pa)); // 4个字节
	printf("%d\n", sizeof(pc)); // 4个字节
	printf("%d\n", sizeof(pf)); // 4个字节

	return 0;
}

② pointer type

1. Pointer type: determines how many bytes (authority) to access when dereferencing the pointer
2. The pointer reference byte of char * accesses 1 byte
3. The pointer reference byte of int * accesses 4 words Section
4. Double * pointer reference byte access 8 bytes

#include <stdio.h>
int main()
{
	// 2个16进制位,可以换算8个二进制位
  //十六进制数的进率是16,二进制数的进率是2,且16=2^4,说明二进制数连续进位4次,等效于16进制数进1位。
 //这么说可能不好理解,那么举个例子吧,比如15+1=16,用二进制表示就是1111+1=10000,用十六进制表示就是F+1=10。
// 这也就说明了一位十六进制数对应四位二进制数了(F对应1111)。
	int a = 0x11223344;
	int *pa=&a;
	*pa = 0;
	//1. 指针类型:决定了指针进行解引用操作的时候,访问几个字节(权限)
	// char * 的指针引用字节访问1个字节
	// int * 的指针引用字节访问4个字节
	// double * 的指针引用字节访问8个字节
	return 0;
}

According to the above code, we debug to see the result:

     We found that int a = 0x11223344 in the int type occupies exactly four bytes. When assigning 0 to a, the four bytes changed from 44 33 22 11 to 00 00 00 00. At this time, the Int type is very satisfying condition.

#include <stdio.h>
int main()
{
	int a = 0x11223344;
	//int *pa=&a;
	//*pa = 0;
	char* pc = &a;  
	*pc = 0;
	//1. 指针类型:决定了指针进行解引用操作的时候,访问几个字节(权限)
	// char * 的指针引用字节访问1个字节
	// int * 的指针引用字节访问4个字节
	// double * 的指针引用字节访问8个字节
	return 0;
}

     At this time, debug again and find that an error has occurred, as shown in the figure:

     It was found that in the char type, only one byte was changed, and an error occurred at this time. But this error also lets us know that the pointer type is a meaningful pointer type: it determines how many bytes (permissions) to access when the pointer is dereferenced.

③Print the entire array element with a pointer

#include <stdio.h>
int main()
{
	int a[10] = { 0 };  //内存中创建了10个整型

	int* pa = a;  //数组名就是首元素的地址
	for (int i = 0; i < 10; i++)
	{
		*pa = i + 1;   // 数值+1
		 pa++;         //地址+1
		 //*(pa+i)=i+1
		 // pa表示pa中存放的地址  *pa表示:指向存放地址的内容,也就是解用
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
	return 0;
}

 

#include <stdio.h>
int main()
{
	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
	//printf("%d\n", a);
	//printf("%d\n", &a[0]);   //首地址
	int* p = a;
	int sz = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", *p);// 1 2 3 4 5 6 7 8 9 10
		p++;
	}
	return 0;
}

 (4) Pointer arithmetic

 ① pointer + - integer

#include <stdio.h>
int main()
{
	int a[5] = { 0 };
	int* pa = a;
	for (int i = 0; i < 5; i++)
	{
		printf("%d ", *(pa + i));  // 0 0 0 0 0
	}
	return 0;
}

② pointer - pointer

#include <stdio.h>
int main()
{
	int a[10] = { 0 };
	printf("%d\n", &a[9] - &a[0]);  // 9
	printf("%d\n", &a[0] - &a[9]);  // -9
	// 两个指针相减的前提是:指针指向同一块连续的空间
	return 0;
}

   The premise of subtracting two pointers is that the pointers point to the same continuous space

   counter (pointer - pointer)

#include <stdio.h>
#include <string.h>
//计数器的方法
// 指针-指针
int my_strlen(char *str)
{
	char* start = str;   // 将初始指针保存
	while (*str != '\0')
	{
		str++;
	}
	return str - start;  // 末尾指针-初始指针
}
int main()
{
	char a[] = "abcdef";
	int len = my_strlen(a);
	printf("%d\n", len);   // 6
	return 0;
}

(5) 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.

#include <stdio.h>
int main()
{
	int a = 10;
	int* p = &a;    //p就是指针变量 ,一级指针变量
	int** pp = &p;  // pp就是二级指针,把一级指针变量的地址取出来,放在pp里面去,pp用两解操作符
	return 0;
}

Note: 1. Here, because the type of a is int, * should point to int for P, so there is int *p
           2. Similarly, the type of p is (int *), so for pp, * should point to (int *) , there will be int* *p 

The use of secondary pointers

#include <stdio.h>
int main()
{
	int a = 10;
	int* p = &a;
	int** pp = &p;
	*p = 20;
	printf("%d ", a); //20
	*(* pp) = 30;
	printf("%d ", a);  //30
	return 0;
}

(6) pointer array

    An array of pointers is: an array, an array of pointers

int main()
{
	// 整型数组存放整型的数组
	int a[10];
	// 字符型数组存放字符的数组
	char b[10];
	// 指针数组存放指针的数组
	int* arr[5]; //存放整型指针的数组
}

    application

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	int c = 30;
	int d = 40;
	int f = 50;
	int* arr3[5] = { &a,&b,&c,&d,&f };
	for (int i = 0; i < 5; i++)
	{
		printf("%d ", (*arr3[i])); // 10 20 30 40 50
	}
	return 0;
}

    Usually used for two-dimensional arrays

#include <stdio.h>
int main()
{
	// 用一维数组模拟二维数组
	int arr1[] = { 1,2,3,4,5 };
	int arr2[] = { 2,3,4,5,6 };
	int arr3[] = { 3,4,5,6,7 };
	int arr4[] = { 4,5,6,7,8 };
	int *arr[4]={arr1, arr2, arr3, arr4}; //指针数组
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			//printf("%d ", arr[i][j]);
			printf("%d ", *(*(arr + i) + j));  // 不可看作二级指针
		}
		printf("\n");
	}
	return 0;
}

Three, mutual encouragement

    This is my primary understanding of pointers. If you don’t understand, or you have questions, you can tell them in the comment area, let’s discuss together, and let’s work together! ! !

 

Guess you like

Origin blog.csdn.net/weixin_45031801/article/details/127017272
Recommended