[C language] The concept of pointers (the distinction between null pointers, wild pointers, and untyped pointers) and application (as parameters, as return values)

Article Directory

One, pointers and pointer variables

1. The concept of pointers

2. Definition of pointer variables

 3. Initialization of pointer variables

4. Reference to pointer variables

5. Discrimination of Null Pointer, Wild Pointer, and Typeless Pointer

Two, pointers as function parameters


One, pointers and pointer variables

1. The concept of pointers

int  a=10;

This line of code defines a variable a of type int, and the compiler allocates 4 bytes of continuous storage space for this variable.

If the first address of this continuous storage space is 0x0037FBCC, then this variable will occupy the four-byte space of 0x0037FBCC~0x0037FBD0.

0x0037FBCC is the address of this variable, because the storage space where the variable is located can be found through this address, and this address is the pointer to the variable .

2. Definition of pointer variables

The pointer indicates the storage space where a certain variable is located, and the corresponding pointer variable stores this pointer. The syntax format for defining pointer variables is as follows:

Variable type* Variable name

The variable type is the type of data that the pointer points to. The "*" before the variable name indicates that the variable is a pointer

E.g:

int *p;//Define a pointer variable p of type int*

 3. Initialization of pointer variables

There are two types of pointer variable assignments:

①The address of the receiving variable is assigned to it

int  a=10;

int *p;

p=&a;//Make the pointer variable p of type int* point to the storage space where the variable a of type int is located

[Note] The ampersand is an address operator, which is used to obtain the address of variable a.

②Point to the same storage space with other pointer variables

int *q;

p=q;//Make int* type pointer variables p and q point to the same storage space

You can also assign a value to the pointer variable at the same time as the definition

int a=10;

int* p=&a;//Define a pointer variable p and initialize it to the address of variable a

4. Reference to pointer variables

The reference of the pointer variable is to access the variable corresponding to the changed address according to the address stored in the pointer variable.

The syntax format of the variable pointed to by the pointer in the pointer variable is as follows:

*Pointer variable

E.g:

int a=10;

int * p=&a;

printf("%d\n",*p); //*p is the data stored in the address pointed to by the pointer variable p

5. Discrimination of Null Pointer, Wild Pointer, and Typeless Pointer

Null pointer : refers to a pointer that does not point to any storage unit. When we need to use pointers, but are not sure where the pointer points to.

int* p1=0; //0 is the only data that can be assigned to a pointer without conversion. In ASCII encoding, the character numbered 0 is null

int* p2=NULL; //NULL is a macro definition, the function is the same as 0

In programming, the pointer is generally initialized to null first, and then assigned to it.

int x=10;

int* p=NULL;//Pointer points to null

p=&x;

Wild pointer : a pointer to an unusable area. Unpredictable errors may occur when operating on wild pointers.

There are two reasons for the formation of wild pointers:

1) The pointer variable is not initialized. If the defined pointer variable is not initialized, it may point to any storage space in the system. If the pointed storage space is in use, when a call occurs and an operation is performed, the system may crash. Therefore, when defining a pointer, it should point to Legal space.

2) If two pointers point to a piece of storage space, after the pointer and memory are used up, the corresponding function is called to release a pointer and the memory it points to, but the other pointer is not changed, and it is made empty. The pointer that has not been released at this time becomes a wild pointer.

Untyped pointer : Before introducing the pointer is determined by the type, such as int *, char * type and the like, but sometimes can not be given a clear pointer type definition, then it uses untyped pointer (void *), which The pointer points to a piece of memory, but because its type is uncertain, the program cannot allocate storage space for the variable pointed to by the pointer according to this definition, so if you want to use the pointer to assign values ​​to other pointers, you must first convert it to other types of pointers. When using this pointer to receive other pointers, it does not need to be forced.

void* p=NULL,*q;

int* m=(int*)p; //Forcibly convert untyped pointer p to int* type and then assign

int a=10;

q=&a; //Receive other types of pointers without forced conversion


Two, pointers as function parameters

In the C language, only one-way value transfer from the actual parameter to the formal parameter is allowed. Why is that?

This is related to the way of memory allocation in C language. When a function call occurs, the system uses the actual parameter corresponding to the formal parameter to assign a value to the formal parameter. At this time, the formal parameter and the variables in the function are stored in the space opened by the system in the stack area during the function call. It is allocated when the function is called, and is recycled when the function is called. In this process, the stack area is invisible to the calling function, so the calling function cannot read the data of the formal parameters in the stack. If you want to pass the data in the stack to the calling program, you can only use return.

Not all the data passed in from the calling function does not need to be changed. If you need to change it, use pointer variables as the parameters of the function. By passing the address, the formal parameters and actual parameters point to the address of the data in the calling program. So that the called function can operate on the data of the main calling function.

case study:

#include <stdio.h>


void swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
	printf("交换函数结果:a=%-5d b=%-5d\n", *a, *b);
}
int main() {
	int a = 3, b = 4;
	swap(&a, &b);
	printf("主函数交换结果:a=%-5d b=%-5d\n", a, b);
}

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/108943693
Recommended