Pointer VS Pointer Variable


Preface

dormitory
   Suppose you are the supervisor sent by your school to supervise the construction of the dormitory building. Today you went to the dormitory building and took a look around. When you walked to the door downstairs, the worker in the red frame found that your key had fallen out of the dormitory and called you to go over it, but it was new My dormitory hasn't been renovated, and there is no dormitory number. What should I do? You can only look up one by one from the first room on the first floor. This search efficiency is particularly low. If there is a dormitory number, the search will be much faster.

1. What is a pointer?

   In order to make it easier to find, we have marked each room in the dormitory with a number. Also in order to facilitate the search, we put a number on each byte in the memory to improve the search efficiency, the so-called "number" here, in the computer, we call it a general address, which has an obvious point For sex, we generally call addresses a pointer .

The pointer is the address .

Two, pointers and pointer variables

1. Pointer

   To understand pointers, we must first know a concept: memory. Memory is a particularly important memory on a computer, and all programs in the computer run in the memory. Therefore, in order to effectively use the memory, the memory is divided into small memory units, each of which is 1 byte in size.
   In order to be able to effectively access each unit of the memory, the memory unit is numbered. These numbers are called the address of the memory unit, and the address is visually called a pointer .

2. Pointer variables

   Variables have addresses (you can use & to get the address of the variable), then how to store the address, you need to define a pointer variable. It is a specialized storage pointer variable to another variable address (i.e. pointer) of the variable . The value of the pointer variable is the address (ie pointer) .

3. The size of the pointer variable

#include <stdio.h>
int main()
{
    
    
	printf("%d\n", sizeof(char*));
	printf("%d\n", sizeof(short*));
	printf("%d\n", sizeof(int*));
	printf("%d\n", sizeof(double*));
	system("pause");
	return 0;
}

32nd place:

The size of the pointer variable in 32 bits
64-bit: The size of the pointer variable in 64 bits Conclusion: The size of the pointer variable is 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform.

4. Pointer VS Pointer Variable

#include<stdio.h>
int main()
{
    
    
	int a = 20, b = 10;     //定义整型变量a,b,并初始化
	int *p1, *p2;    //定义指向整型数据的指针变量p1,p2,‘*’表示指向
	p1 = &a;                //把变量a的地址赋给指针变量p1
	p2 = &b;                //把变量b的地址赋给指针变量p2
	printf("&a=%p &b=%p\n", &a, &b);//输出变量a,b的地址
	printf("p1=%p p2=%p\n", p1, p2);//输出指针变量p1,p2
	system("pause");
	return 0;
}

Insert picture description here
   Through this example, we can better understand pointer variables. Pointer variables have variable names: p1, p2, and variable contents: &a, &b.

Pointer: is an address.
Pointer variable: To open up space, there is a variable name and variable content (the address stored in the pointer variable is the pointer).

Usually we refer to pointers as pointer variables.

Third, the use of pointers

1. Declare a pointer variable

code show as below:



int *p;        // 声明一个 int 类型的指针 p
char *p        // 声明一个 char 类型的指针 p
int *arr[10]   // 声明一个指针数组,该数组有10个元素,其中每个元素都是一个指向 int 类型对象的指针
int (*arr)[10] // 声明一个数组指针,该指针指向一个 int 类型的一维数组
int **p;       // 声明一个指针 p ,该指针指向一个 int 类型的指针


2. Dereference of pointer

The dereference of the pointer represents the target pointed to by the pointer.

code show as below:

#include <stdio.h>
int main()
{
    
    
	int a = 20;
	printf("a=%d\n", a);
	printf("&a=%p\n", &a);
	int *p = &a;
	printf("p=%p\n", p);
	printf("*p=%d\n", *p);//解引用
	system("pause");
	return 0;
}


Using pointer dereference, assign a value of 10.

#include<stdio.h>
int main()
{
    
    
	int a;
	int *p = &a;     //定义一个整型指针p,并且p指向了a的地址
	*p = 10;        //解引用,即给a赋值10;
	printf("a=%d\n", a);
	system("pause");
	return 0;
}

result

Guess you like

Origin blog.csdn.net/qq_34270874/article/details/109181990