C language pointer operation eight summary

insert image description here

The biggest reason for learning is to get rid of mediocrity. One day earlier, there will be more splendor in life; one day later, one day more mediocrity. 


At the end of the article, a detailed summary of the eight major 


study diary

Table of contents

study diary

ForewordEdit

1. The meaning of the pointer 

2. An address-type data actually contains 3 pieces of information 

3. Inductive comparison of pointer variables

4. Pointer arithmetic 

Add (subtract) an integer to a pointer variable

pointer variable assignment

Two pointer variables can be subtracted

Comparing two pointer variables

empty value

Five, the advantages of the pointer

C language pointer manipulation


foreword

        The pointer is divided into eight small parts here, namely "pointer variable", " array referenced by pointer", "*referred to multidimensional array by pointer", "string referenced by pointer", "*pointer to function", "* Functions Returning Pointer Values", "* Arrays of Pointers and Multiple Pointers", "*Dynamic Memory Allocation and Pointer Variables to It".   

1. The meaning of the pointer 

        "Pointer" is a figurative term in C language, which vividly expresses the relationship of "pointing to", and its physical realization is accomplished through addresses.

&a is the address of variable a, also known as the pointer of variable a.

A pointer variable is a variable that stores an address. It can also be said that a pointer variable is a variable that stores a pointer.

The value of a pointer variable is an address, it can also be said that the value of a pointer variable is a pointer.

A pointer variable can also be called an address variable, and its value is an address.

& is the address operator, &a is the address of a, it can also be said that & is the pointer operator.

&a is a pointer to variable a (that is, a pointer to variable a).

The array name is an address, which is the address of the first element of the array. It can also be said that the array name is a pointer, which is the pointer of the first element of the array.

The function name is a pointer (pointing to the first byte of the function code area), and it can also be said that the function name is an address (the address of the first byte of the function code area).

If the actual parameter of the function is an array name, what is passed to the formal parameter is an address. It can also be said that what is passed to the formal parameter is a pointer.

2. An address-type data actually contains 3 pieces of information 

① Indicates the pure address of the memory number.

② Its own type, that is, the pointer type.

③ What type of data is stored in the storage unit identified by it, that is, the base type.

int a;
/* &a为a的地址,它就包括以上3个信息,它代表的是一个整型数据的地址,int是&a的基类型(即它指向的是int型的存储单元)。&a就是“指向整型数据的指针类型”或“基类型为整型的指针类型”,其类型可以表示为“int *”型。*/

To distinguish between pointers and pointer variables. A pointer is an address, and a pointer variable is a variable used to store an address.

For pointer variables, the address of whose address is stored in the pointer variable is said to be pointed to by the pointer variable.

int a,*p;		//p是int*型的指针变量,基类型是int型 
float b;
p=&a;		//a是int型,合法 
p=&b;		//b是float型,类型不匹配

A void * pointer is a special pointer that does not point to any type of data. If you need to use this address to point to a certain type of data, you should perform type conversion on the address first.

3. Inductive comparison of pointer variables

Variable definitions

type representation

meaning

int i;

int

define an integer variable i

int *p;

int *

Define p as a pointer variable pointing to integer data

int a[5];

int [5]

Define an integer array a with 5 elements

int *p[4];

int *[4]

Define the pointer array p, which consists of 4 pointer elements pointing to integer data

int (*p)[4];

int (*)[4]

p is a pointer variable pointing to a one-dimensional array containing 4 elements

int f();

int ()

f is a function that returns an integer function value

int *p();

int *()

p is a function that returns a pointer to integer data

int (*p)();

int (*)()

p is a pointer to a function that returns an integer value

int **p;

int **

p is a pointer variable that points to a pointer variable pointing to integer data

void *p;

void *

p is a pointer variable, the base type is void (empty type), does not point to a specific object

4. Pointer arithmetic 

Add (subtract) an integer to a pointer variable

p++;		//将该指针变量的原值(是一个地址)和它指向的变量所占用的存储单元的字节数相加

pointer variable assignment

Assign a variable address to a pointer variable. An integer should not be assigned to a pointer variable.

p=&a; 		//将变量a的地址赋给p
p=array; 	//将数组array首元素地址赋给p
p=&array[i];	//将数组array第i个元素的地址赋给p
p=max;		//max为已定义的函数,将max的入口地址赋给p
p1=p2;		//p1和p2是基类型相同指针变量,将p2的值赋给p1

Two pointer variables can be subtracted

If both pointer variables point to elements in the same array, the difference between the values ​​of the two pointer variables is the number of elements between the two pointers.

Comparing two pointer variables

If two pointers point to elements of the same array, they can be compared. A pointer variable pointing to the preceding element is "less than" a pointer variable pointing to the following element. The comparison is meaningless if p1 and p2 do not point to the same array.

empty value

        A pointer variable can have a null value, that is, the pointer variable does not point to any variable. NULL is a symbolic constant representing the integer 0. NULL is defined in the stdio.h header file: #define NULL 0 It makes p point to the unit whose address is 0. The system guarantees that the unit is not used for other purposes (no valid data is stored).

        The value of p is NULL and p is not assigned a value are two different concepts. The former has a value (the value is 0) and does not point to any variable. Although the latter has not assigned a value to p, it does not mean that p has no value, but its value is an unpredictable value, that is, p may point to an unknown variable. specified unit.

Five, the advantages of the pointer

Improve program efficiency;

When the value of the variable pointed to by the pointer changes when the function is called, these values ​​can be used by the calling function, that is, multiple changeable values ​​can be obtained from the function call;

Dynamic memory allocation can be implemented.

If the pointer is used improperly, there will be hidden, difficult to find and troubleshoot faults. Therefore, use pointers with great care.

C language pointer manipulation

C language pointer operation (1) pointer variable

C language pointer operation (2) referencing arrays through pointers

C language pointer operation (3) *Referring to multidimensional arrays through pointers 

C language pointer operation (four) referencing strings through pointers

C language pointer operation (5) * pointer to function

C language pointer operation (6) * function that returns a pointer value

C language pointer operation (7) * pointer array and multiple pointers 

C language pointer operation (8) * dynamic memory allocation and pointer variables pointing to it

Guess you like

Origin blog.csdn.net/m0_63794226/article/details/126617644