What does *amp; mean in a function parameter(*amp; 有什么用?函数参数中的平均值)
问题描述
如果我有一个接受 int *&
的函数,这意味着什么?如何仅将 int 或 int 指针传递给该函数?
If I have a function that takes int *&
, what does it means? How can I pass just an int or a pointer int to that function?
function(int *& mynumber);
每当我尝试传递指向该函数的指针时,它都会说:
Whenever I try to pass a pointer to that function it says:
error: no matching function for call to 'function(int *)'
note: candidate is 'function(int *&)'
推荐答案
它是对 int 指针的引用.这意味着有问题的函数可以修改指针以及 int 本身.
It's a reference to a pointer to an int. This means the function in question can modify the pointer as well as the int itself.
你可以只传递一个指针,一个复杂的问题是指针需要是一个左值,而不仅仅是一个右值,例如
You can just pass a pointer in, the one complication being that the pointer needs to be an l-value, not just an r-value, so for example
int myint;
function(&myint);
单独是不够的,也不允许 0/NULL,如:
alone isn't sufficient and neither would 0/NULL be allowable, Where as:
int myint;
int *myintptr = &myint;
function(myintptr);
可以接受.当函数返回时,myintptr
很可能不再指向它最初指向的内容.
would be acceptable. When the function returns it's quite possible that myintptr
would no longer point to what it was initially pointing to.
int *myintptr = NULL;
function(myintptr);
如果函数希望在给定 NULL 指针时分配内存,也可能有意义.检查随函数提供的文档(或阅读源代码!)以了解如何使用指针.
might also make sense if the function was expecting to allocate the memory when given a NULL pointer. Check the documentation provided with the function (or read the source!) to see how the pointer is expected to be used.
这篇关于*& 有什么用?函数参数中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:*& 有什么用?函数参数中的平均值


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01