Home  >  Article  >  Backend Development  >  Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types

Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types

WBOY
WBOYOriginal
2024-04-27 09:27:01665browse

There are three ways to pass pointer parameters in C: passing by value, passing by reference and passing by address. Passing by value copies the pointer without affecting the original pointer; passing by reference allows the function to modify the original pointer; passing by address allows the function to modify the value pointed to by the pointer. Choose the appropriate parameter transmission method according to your needs.

C++ 函数参数详解:不同指针类型的传参方式对比

C Detailed explanation of function parameters: Comparison of parameter passing methods of different pointer types

Function parameter passing is an important method in C Important programming concept that allows passing a value or address in a function call. For pointer types, C provides several different methods of passing parameters. This article will compare these methods in detail through practical cases.

1. Pointer value passing

The syntax for passing pointer value is void foo(int* ptr);, which effectively transfers the pointer value to A copy is passed to the function. Modifications to the copy within the function do not affect the original pointer.

#include <iostream>

void foo(int* ptr) {
    std::cout << *ptr << std::endl; // 输出:5
    *ptr = 10; // 仅修改函数内的副本
}

int main() {
    int num = 5;
    foo(&num); // 传递指针值
    std::cout << num << std::endl; // 输出:5,原始指针未修改
    return 0;
}

2. Pointer by reference

The syntax for passing pointer reference isvoid foo(int*& ptr);, which will Reference is passed to the function. Modifications to the reference within the function will affect the original pointer.

#include <iostream>

void foo(int*& ptr) {
    std::cout << *ptr << std::endl; // 输出:5
    *ptr = 10; // 修改原始指针
}

int main() {
    int num = 5;
    foo(&num); // 传递指针引用
    std::cout << num << std::endl; // 输出:10,原始指针已修改
    return 0;
}

3. Pointer to address

The syntax for passing pointer address is void foo(int** ptr);, which will The address is passed to the function. Only pointers can be accessed within functions, and the value they point to cannot be directly accessed.

#include <iostream>

void foo(int** ptr) {
    std::cout << *(*ptr) << std::endl; // 输出:5
    *(*ptr) = 10; // 修改指针指向的值
    *ptr = nullptr; // 指向另一个值
}

int main() {
    int num = 5;
    foo(&num); // 传递指针地址
    std::cout << num << std::endl; // 输出:10,指针指向的值已修改
    std::cout << *foo(&num) << std::endl; // 输出:0,指针指向另一个值
    return 0;
}

Summary

Pointer-to-value copy pointer does not affect the original pointer. Pointer by reference allows a function to modify the original pointer, while pointer by address allows a function to modify the value pointed to by the pointer. It is crucial to choose the appropriate parameter transmission method according to specific needs.

The above is the detailed content of Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn