Home > Backend Development > C++ > body text

The difference between reference parameters and pointer parameters in C++ functions

WBOY
Release: 2024-04-20 21:21:02
Original
1051 people have browsed it

In the C function, the reference parameter passes the variable address, and the modification of the parameter affects the original variable, while the pointer parameter passes the pointer to the address, and the modification of the parameter does not affect the original variable.

C++ 函数中引用参数和指针参数的区别

The difference between reference parameters and pointer parameters in C functions

In C, a function can accept reference parameters or pointer parameters. Although both are used to pass the address of a variable, there are some key differences between them.

Reference parameters

Reference parameters pass the address of the variable through the symbols &. It essentially passes the variable itself, meaning any changes made to that reference parameter are reflected in the original variable.

Code example:

void swap(int& a, int& b) {
  int temp = a;
  a = b;
  b = temp;
}

int main() {
  int x = 5;
  int y = 10;
  swap(x, y);  // 交换 x 和 y 的值
  cout << x << " " << y << endl; // 输出:10 5
}
Copy after login

Pointer parameters

Pointer parameters pass variables through the symbols * address. It essentially passes a pointer to the memory address of the variable, which means that any changes made to the pointer parameter are not reflected in the original variable.

Code sample:

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

int main() {
  int x = 5;
  int y = 10;
  swap(&x, &y);  // 交换 x 和 y 的值
  cout << x << " " << y << endl; // 输出:5 10
}
Copy after login

Difference summary

FeaturesReference parametersPointer parameters
Transmission method Reference variable addressPass pointer address
Modification of parametersChange original variableNo Will change the original variable
Memory usagePointer sizeReference size
Purpose Pass actual parametersPass large objects or complex structures

Practical case

Consider a need Function that swaps two elements.

Use reference parameters:

void swap(int& a, int& b) {
  int temp = a;
  a = b;
  b = temp;
}
Copy after login

Use pointer parameters:

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
Copy after login

Which method is better depends on the involved specific situation. If two simple values ​​need to be exchanged, reference parameters are more appropriate. However, if you need to exchange large objects or complex structures, a pointer parameter is more suitable as it avoids copying large chunks of data inside and outside the function.

The above is the detailed content of The difference between reference parameters and pointer parameters in C++ functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template