C language - pointer (detailed)

pointer (detailed)

1. Basic introduction to pointers


image-20221030161854436


2. What is a pointer


A pointer is a variable whose value is the address of another variable ( as explained in the previous diagram ), that is, the address of the memory location

direct address. Just like any other variable or constant, before using a pointer to store the address of another variable, it is

statement. The general form of a pointer variable declaration is:

int *ip; /* 一个整型的指针 */
double *dp; /* 一个 double 型的指针 */
float *fp; /* 一个浮点型的指针 */
char *ch; /* 一个字符型的指针 */

image-20221030162354356


3. Arithmetic operations on pointers

(1) Pointer increment operation

#include <stdio.h>

const int MAX=3;
int main(){
    
    
	int var[] = {
    
    10,100,200};
	int i,*ptr;//ptr是一个int*指针
	ptr = var;//ptr 指向了var数组的首地址
	for(i=0;i<MAX;i++){
    
    
		printf("var[%d]地址=%p \n",i,ptr);
		printf("存储值:var[%d]=%d\n",i,*ptr);
		ptr++;//ptr=ptr+1(1个int字节数)
	}
	getchar();
	return 0;
}

image-20221030162613746

数组在内存中是连续分布的
当对指针进行++时,指针会按照它指向的数据类型字节数大小增加,比如 int * 
指针,每++ , 就增加4个字节

(2) Pointer decrement operation

#include <stdio.h>

const int MAX=3;
int main(){
    
    
	int var[] = {
    
    10,100,200};
	int i,*ptr;//ptr是一个int*指针
	ptr = &var[MAX-1];//var2的地址
	for(i=MAX;i>0;i--){
    
    //反向遍历
		printf("var[%d]地址=%p \n",i-1,ptr);
		printf("存储值:var[%d]=%d\n",i-1,*ptr);
		ptr--;//ptr=ptr+1(1个int字节数)
	}
	getchar();
	return 0;
}

image-20221030162757417

(3) pointer + - operation

#include <stdio.h>

int main () {
    
    
	int var[] = {
    
    10, 100, 200};
	int i, *ptr;
	ptr = var;
	ptr += 2; //ptr的存储的地址 + 2个int的字节(8个字节)
	printf("var[2]=%d\nvar[2]的地址=%p\nptr存储的地址=%p\nptr指向的地址的内容=%d", 
	var[2], &var[2], ptr, *ptr);
	getchar();
	return 0; 
}

image-20221030163301962

(4) Comparison of pointers

Pointers can be compared using relational operators such as ==, < <=, and > >=. If p1 and p2 point to two variables, such as different elements in the same array, you can compare the size of p1 and p2, see the following code, what is the output?

image-20221030173032604


4. Array of pointers [emphasis]

(1) Basic introduction

To make the elements of the array point to the addresses (pointers) of int or other data types. Array of pointers can be used

(2) Pointer array definition

* Data type pointer array name [size];

Quick start example:


image-20221030174620262

Schematic diagram of the memory layout:

image-20221030174735705


(3) Application case of pointer array

Please write a program to define an array of pointers to characters to store string lists (titles of four famous books), and display string information by traversing the array of pointers, (ie: define an array of pointers, each element of the array , pointing to a string)

books[0] = 三国演义
books[1] = 西游记
books[2] = 红楼梦
books[3] = 水浒传

image-20221030212345016

5. Pointers to pointers (multiple pointers)

A pointer to a pointer is a form of multi-level indirection , or a chain of pointers. Usually, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, and the second pointer points to the location containing the actual value (as shown below)

image-20221030213421016

(1) A pointer variable that points to a pointer must be declared as follows, that is, two asterisks are placed before the variable name. For example, the following
declares a pointer to a pointer of type int: int **ptr; // ptr is of type int **

(2) When a target value is indirectly pointed to by a pointer to another pointer, accessing this value requires the use of two asterisk operators, such as **ptr

(3) Case demonstration + memory layout diagram

image-20221030222521463

image-20221030223557477

6. Pass the pointer (address) to the function

When the formal parameter type of the function is a pointer type, when using the function, it is necessary to pass a pointer, or address, or an array to the formal parameter, for example

Case 1: Pass address or pointer to pointer variable

image-20221030225716588

The memory layout diagram is as follows:

image-20221030225645267

Continuous updates are added. .

Guess you like

Origin blog.csdn.net/m0_53415522/article/details/127607200