Home  >  Article  >  Backend Development  >  The difference between reference parameters and pointer parameters in C++ functions

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

WBOY
WBOYOriginal
2024-04-20 21:21:021001browse

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
}

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
}

Difference summary

Features Reference parameters Pointer parameters
Transmission method Reference variable address Pass pointer address
Modification of parameters Change original variable No Will change the original variable
Memory usage Pointer size Reference size
Purpose Pass actual parameters Pass 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;
}

Use pointer parameters:

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

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!

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