Pointer (1) basics

Pointer (1) Basic

The concept of a pointer

  1. In order to facilitate access to the contents of the memory, a number is given to each memory cell.We call this number an address and a pointer.
  2. Pointers are also a data type, so pointers have their own memory to store addresses (numbers)

The four elements of pointer

  1. The type of the pointer itself removes the pointer name, and what remains is the type of the pointer itself
  2. The type pointed to by the pointer removes the pointer name and a *, and the rest is the type pointed to by the pointer
  3. The memory of the pointer itself is used to store a number (4 bytes)
  4. The memory pointed to by the pointer can be of various types
    int num = 0;          // 类型: int
    int arr[10] = {
    
    };     // 类型: int [10]
    int* MuShan = {
    
    1,2,3};
    //去掉名字,剩下的就是类型

Definition of two pointers

1 operator

*: When it is defined, it means that the definition is a pointer. Other times it means that the reference is resolved.

&: Take address character (used to take address)

2 definition

There are many ways to understand the definition of pointers;

  1. Type variable name;
int* p; // 类型: int
        // 变量名: *p
  1. The pointer name of the pointer itself;
int* p; // 指针本身的类型: int*
        // 指针名: p
  1. The type pointed to by the pointer * pointer name;
int* p; // 指针指向的类型: int
        // 指针名: p

push back:

  1. Remove the pointer name, what remains is the type of the pointer itself
  2. Remove the pointer name and a *, the rest is the type pointed to by the pointer
int****** p1; // p1本身的类型: int****** 
              // p1指向的类型: int*****  (6级指针可以指向5级指针)

Three pointer memory

  1. All pointers, regardless of type, occupy 4 bytes of memory in memory (the address is stored) (specifically related to the 64/32-bit environment)
#include <stdio.h>
int main()
{
    
    
	char*       pch;
	short*      psh;
	int*        pn;
	float*      pf;
	double*     pd;
    
	printf("%d\n", sizeof(pch));  // 4
	printf("%d\n", sizeof(psh));  // 4
	printf("%d\n", sizeof(pn));   // 4
	printf("%d\n", sizeof(pf));   // 4
	printf("%d\n", sizeof(pd));   // 4
    
	return 0;
}
  1. Point to start address
int num = 10;
int* p = &num;

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-52hvEuyS-1613538026451) (C:\Users\admin\Desktop\Six-Star Education\Learning\Pointer\Pointer ( A)]\Pointer Basics(1)1-1.png)

Initialization and assignment of four pointers

1 Use the address of the corresponding type variable

	int num = 10;
	int* pn = &num;   //初始化

	float f = 3.14f;
	float* pf;
	pf = &f;          //赋值

2 Use the same type of pointer

	int num = 10;
	int* pn = &num;     // 初始值
	int* p1 = pn;       // 初始化
	int* p2;
	p2 = pn;            // 赋值

3 Use address directly

	int* p = (int*)0x36;

4 Use array names

The first-level pointer can accept a bunch of array names of one-bit arrays

	int arr[5] = {
    
     1,2,3,4,5 };
	int* p = arr;

5 character string

#include <stdio.h>
int main()
{
    
    
	// 数组名就是数组的首地址
	char arr[5] = {
    
     'a','b','c' };
	char* p = arr;
	printf("%s\n",p);     //输出:abc
	char str[5] = "abcd";
	p = str;
	printf("%s\n", str);  //输出:abcd
	printf("%s\n", p);    //输出:abcd
	// ==> char*类型的指针可以直接用来打印整个字符串到'\0'停止
	const char* p1;
	p1 = "1234";
	printf("%s\n",p1);    //输出:1234
	const char* p2 = "Mushan";
	printf("%s\n",p2);    //输出:Mushan
	return 0;
}

6 Leave blank

	int* p = NULL;
	int* p1 = (int*)0x0;
/*
   NULL: #define NULL  0
   0地址
   有时候,指针定义好了,但是暂时没有指向
   或者是,指针用完了,没有指向了
   指针不知道指向哪里(会随机指向)
   此时的指针,很危险(野指针)
   所以 这些情况下的指针
   统一安排一个地址给他们指向
   指向0地址
*/

7 Multi-level pointers

#include<stdio.h>

int main() {
    
    
	int num = 10;
	printf(" num = %d\n", num);        //   num = 10;
	printf("&num = %X\n", &num);       //  &num = 10FFA78
    
	int* p = &num;
	printf("*p = %d\n", *p);           //  *p = 10 (num值)
	printf(" p = %X\n", p);            //   p = 10FFA78 (num的地址)
	printf("&p = %X\n", &p);           //  &p = 10FFA6C

	int** pp = &p; // 一个二级指针
	printf("**pp = %d\n", **pp);       // **pp = 10 (num值)
	printf(" *pp = %X\n", *pp);        //  *pp = 10FFA78 (num的地址)
	printf(" pp = %X\n", pp);          //   pp = 10FFA6C (p的地址)
	printf(" &pp = %X\n", &pp);        //  &pp = 10FFA60 

	int*** ppp = &pp; // 一个三级指针
	printf("***ppp = %d\n", ***ppp);   // ***ppp = 10 (num值)
	printf(" **ppp = %X\n", **ppp);    //  **ppp = 10FFA78 (num地址)
	printf(" *ppp = %X\n", *ppp);      //   *ppp = 10FFA6C (p的地址)
	printf("  ppp = %X\n", ppp);       //    ppp = 10FFA60 (pp的地址)
	printf(" &ppp = %X\n", &ppp);      //   &ppp = 10FFA54

	return 0;
}

Five pointer addition and subtraction

Core: The value (pointing) of the pointer itself has not changed

Pointer offset

  1. Pointer can add or subtract an integer

  2. After the pointer is added or subtracted by an integer, it is actually offset

  3. The range of the offset is an integer number of units plus or minus

    Unit: The number of bytes in the memory of the type pointed to by the pointer

    Offset: The pointer points unchanged, but the content can be taken according to the offset

#include <stdio.h>
int main()
{
    
    
	int num = 10;
    
	int* p = &num;
	printf("%X\n", p);          // EFFB5C
	printf("%X\n", p + 1);      // EFFB60
    
	return 0;
}

Increase and decrease of six pointers

Self-increment and self-decrement, will change the pointer point

++: Indicates that the pointer moves backward by one unit

–: Indicates that the pointer moves forward one unit

Unit: The number of bytes in the memory occupied by the type pointed to by the pointer

#include <stdio.h>
int main()
{
    
    
	int num = 10;
	int* p = &num;
	printf("%X\n", p);          // EFFB5C
	printf("%X\n", p + 1);      // EFFB60

	printf("%d\n",*p);          // 10
	printf("%X\n", p += 1);     // EFFB60
	printf("%d\n",*p);          // -858993460(无意义)
	return 0;
}

Seven traversing the array through pointers

Traverse a one-dimensional array

#include <stdio.h>
int main()
{
    
    
	int arr[10] = {
    
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%2d", arr[i]);
	}
	printf("\n");
	int* p = arr;
	// p和arr,除了arr是一个常量之外,其他几乎是一样的
	for (size_t i = 0; i < 10; i++)
	{
    
    
		printf("%2d", p[i]);
	}

	printf("\n");
	printf("%d\n", p[0]);        // 0
	printf("%d\n", *(p + 0));    // 0
	printf("%d\n", p[1]);        // 1(先偏移 后取值)
	printf("%d\n", *(p + 1));    // 1
	// p[n] <==> *(p+n)
	return 0;
}
// p[n]:叫做下标形式
// *(p+n):叫做指针偏移的形式

Traverse a two-dimensional array

  1. Two-dimensional arrays are also arrays

  2. A two-dimensional array can be regarded as a one-dimensional array whose elements are a one-dimensional array

  3. The memory of the array is contiguous

#include<stdio.h>

int main()
{
    
    
	int arr[3][4] = {
    
    
        {
    
    1,2,3,4},
		{
    
    5,6,7,8},
		{
    
    9,10,11,12}
	};

	for (size_t i = 0; i < 3; i++)
	{
    
    
		for (size_t j = 0; j < 4; j++)
		{
    
    
			printf("%3d", arr[i][j]);
		}
		printf("\n");
	}

	int* p0 = arr[0];
	int* p1 = arr[1];
	int* p2 = arr[2];
	printf("\n");
	// 1:
	for (size_t i = 0; i < 4; i++)
	{
    
    
		printf("%3d", p0[i]);           // 1  2  3  4
	}
	printf("\n");
	for (int i = -4; i <= 7; i++)
	{
    
    
		printf("%3d", p1[i]);           // 1  2  3  4  5  6  7  8  9 10 11 12
	}
	printf("\n");
	for (int i = 0; i < 12; i++)
	{
    
    
		printf("%3d", arr[0][i]);       // 1  2  3  4  5  6  7  8  9 10 11 12
	}
	printf("\n");
	// 下标: 保证数组不越界即可
    
	// 2:
	int* p = &arr[0][0];
	for (int i = 0; i < 12; i++)
	{
    
    
		printf("%3d", arr[0][i]);       // 1  2  3  4  5  6  7  8  9 10 11 12       
	}
	printf("\n");
	for (int i = 0; i < 12; i++)
	{
    
    
		printf("%3d", *p);              // 1  2  3  4  5  6  7  8  9 10 11 12
		p++;
	}
	printf("\n");
	return 0;
}

Guess you like

Origin blog.csdn.net/zhuiyizhifengfu/article/details/113833241