The usage and corresponding functions of pointers in C language

Table of contents

What is a pointer?

Basic pointer operations

(1) Variable address reference:

(2) Dynamic memory allocation:

(3) Array and string operations:

(4) Function parameter transfer:

(5) Data structure operations:

(6) NULL pointer:

Advantages of using pointers

1. The pointer directly accesses the memory address:

2. Implementation of complex data structures:

3. Function transfer parameters and modify variable values:

4. Dynamic memory allocation and release:

5. Direct hardware control:


When talking about pointers in C, we enter a core and powerful area of ​​programming. Pointers are a basic concept in the C language, which provide programmers with the ability to directly access memory and perform various operations. In this blog, we will delve into the usage of pointers in C language and their corresponding functions.

What is a pointer?

In C language, a pointer is a variable, but it does not store a regular value, but stores a memory address. This memory address can point to the location of other variables or data structures. The essence of a pointer is a reference, which allows us to operate data in memory, access variables or allocate dynamic memory.

Basic pointer operations

Below we will introduce the basic operations of pointers in C language and their functions:

(1) Citation of change land:

One of the most basic functions of a pointer is to reference the memory address of a variable. This means you can use a pointer to get the address of a variable and then manipulate the data at that address.

  • int a = 42; 
    int *ptr = &a; // ptr存储了变量a的地址
    

This is useful for data passing and reference passing,allowing the function to modify the caller's variable value

(2) Dynamic memory allocation:

The pointer is also used for dynamic memory allocation. C language provides functions such as malloc(), calloc(), and realloc() for allocating memory at runtime. This is a powerful feature of pointers because it allows a program to dynamically allocate and free memory when needed.

int *dynamicArray = (int *)malloc(5 * sizeof(int)); // 动态分配一个整数数组 

free(dynamicArray); // 释放内存以避免内存泄漏

Dynamic memory allocation is useful for working with data structures of uncertain size, such as linked lists and trees.

(3) Array and string operations:

Pointers are often used with arrays and strings. Pointers can be used to iterate over array elements or characters in a string, as well as modify them.


cchar str[] = "Hello"; 
char *ptr = str; 
while (*ptr != '\0') { 
    printf("%c", *ptr); 
    ptr++; 
}

This is useful for string processing and text manipulation.

(4) Function parameter transfer:

By passing a pointer as a function parameter, you can pass a reference, allowing variables in the calling function to be modified inside the function.


void modifyValue(int *x) {
     *x = 20; // 修改x指向的值 
} 

int main() { 
    int num = 10; 
    modifyValue(&num); // 传递num的地址 
    printf("num = %d", num); // num现在的值为20 return 0; 
}

This passing by reference is useful for sharing data between functions withouthaving to pass large copies of the data.

(5) Data structure operations:

In the implementation of complex data structures, such as linked lists, trees, or graphs, pointers are used to connect and access different elements.

struct Node { int data; struct Node *next; };

Build flexible data structures for use in a variety of applications.

(6) NULL finger button:

The pointer can point toNULL, which means the pointer does not point to any valid memory address. This is useful in condition checking and error handling.

int *ptr = NULL; 
if (ptr == NULL) { 
    printf("指针为空");
}

This can help avoid errors caused by dangling pointers and accessing invalid memory addresses.

Note that you need to be particularly careful when using pointers. Incorrect memory accesses (such as null pointer dereferences or out-of-bounds accesses) can cause program crashes or undefined behavior. Always make sure you assign a valid memory address to a pointer before using it, and release the memory promptly when you are done using it.

Advantages of using pointers

1. The pointer directly accesses the memory address:

Pointers allow direct access to memory addresses, which eliminates many layers of abstraction and increases the efficiency of accessing data. Especially when large data structures or memory areas need to be frequently manipulated, pointers can significantly improve program performance.

2. Implementation of complex data structures:

Pointers are the key to implementing complex data structures. Data structures such as linked lists, trees, and graphs usually use pointers to connect various elements to achieve flexible data organization and efficient data access. Pointers allow you to dynamically allocate and connect blocks of data in memory, which is very useful for managing non-contiguous data.

3. Function transfer parameters and modify variable values:

By passing a pointer as a function parameter, you can implement pass-by-reference, thereby modifying the value of the passed variable inside the function. This is useful for situations where you need to modify the value of an external variable inside a function. By passing a pointer, the function can modify the original variable instead of its copy, which allows more complex operations.

4. Dynamic memory allocation and release:

Pointers can be used to dynamically allocate memory, which is useful for allocating memory as needed at runtime. You can manage memory using functions such as malloc and free (in C) or new and delete (in C++). This reduces wasted memory and allows higher level data structures.

5. Direct hardware control:

In embedded programming and system-level programming, pointers allow direct access to hardware registers and memory-mapped devices, allowing precise control of the underlying hardware. This is an important tool for creating operating systems, drivers, and embedded systems.

It is important to note that while pointers play an important role in programming, they also come with potential problems such as dangling pointers, memory leaks, and illegal memory accesses. Therefore, you need to be careful when using pointers to ensure that memory is allocated and freed correctly to avoid potential errors.

Guess you like

Origin blog.csdn.net/m0_74053536/article/details/134274841