C++:通过参考传递数组
我正在尝试定义一个函数原型,它采用不同长度的 char 数组。我知道我必须通过引用传递数组,以避免数组衰减为指向其第一个元素的指针。因此,我一直在研究这个简单的示例,以确保我的理解正确。
#include <stdio.h> // size_t
//template to accept different length arrays
template<size_t len>
//pass array of char's by reference
void func(const char (&str)[len])
{
//check to see if the array was passed correctly
printf(str);
printf("\n");
//check to see if the length of the array is known
printf("len: %lu",len);
}
int main(){
//create a test array of chars
const char str[] = "test12345";
//pass by reference
func(&str);
return 0;
}
这给了我编译器错误:
main.cpp: In function ‘int main()’:
main.cpp:19:14: error: no matching function for call to ‘func(const char (*)[10])’
func(&str);
^
main.cpp:6:6: note: candidate: template<long unsigned int len> void func(const char (&)[len])
void func(const char (&str)[len])
^~~~
main.cpp:6:6: note: template argument deduction/substitution failed:
main.cpp:19:14: note: mismatched types ‘const char [len]’ and ‘const char (*)[10]’
func(&str);
我认为函数签名 func(const char (&str)[len]) 表示指向长度 char
数组的指针code>len,这就是我通过 func(&str)
传递的内容。
我尝试了 func(str) ,但我认为这是错误的,因为我传递的是值 str,而不是它的引用。然而,这实际上有效,我不明白为什么。
这是怎么回事? 通过引用传递实际上意味着什么?
I am trying to define a function prototype which takes an array of char
of different lengths. I understand that I must pass the array by reference to avoid the array decaying to a pointer to its first element. So I've been working on this simple example to get my understanding correct.
#include <stdio.h> // size_t
//template to accept different length arrays
template<size_t len>
//pass array of char's by reference
void func(const char (&str)[len])
{
//check to see if the array was passed correctly
printf(str);
printf("\n");
//check to see if the length of the array is known
printf("len: %lu",len);
}
int main(){
//create a test array of chars
const char str[] = "test12345";
//pass by reference
func(&str);
return 0;
}
This gives me the compiler errors:
main.cpp: In function ‘int main()’:
main.cpp:19:14: error: no matching function for call to ‘func(const char (*)[10])’
func(&str);
^
main.cpp:6:6: note: candidate: template<long unsigned int len> void func(const char (&)[len])
void func(const char (&str)[len])
^~~~
main.cpp:6:6: note: template argument deduction/substitution failed:
main.cpp:19:14: note: mismatched types ‘const char [len]’ and ‘const char (*)[10]’
func(&str);
I thought that the function signature func(const char (&str)[len])
indicates a pointer to a char
array of length len
, which is what I am passing by func(&str)
.
I tried func(str)
, which I would expect to be wrong, since I am passing the value str, instead of its reference. However, this actually works and I dont understand why.
What is going on here?
What does it actually mean to pass by reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的功能已正确声明,但是您没有正确地将数组传递给它。
func(*str);
首先将数组衰减到指向第一个元素的指针,然后延迟指针,因此将第一个字符传递到func()
。但是没有定义func(char)
函数,因此这是一个错误。func(&amp; str);
占据了数组的地址,因此将指针传递给数组,而不是对其的引用。但是没有定义的func(char(*)[len])
函数,因此这也是一个错误。要通过参考传递
str
,您只需通过str
as-as-as-is-is-is-i*
或&amp;
:<代码> func(str);
这与对任何其他类型的变量的引用,例如:
在侧面注:
printf(str);
是危险的,因为您不知道str
是否包含任何%
字符。更安全的调用是:printf(“%s”,str);
or:
puts(str);
,但是仅当
str
>是无效的终止(在您的情况下)。甚至更安全的是:printf(“%。s”,(int)len,str);
不需要零终端。
Your function is declared correctly, but you are not passing the array to it correctly.
func(*str);
first decays the array to a pointer to the 1st element, and then deferences that pointer, thus passing just the 1st character tofunc()
. But there is nofunc(char)
function defined, so this is an error.func(&str);
takes the address of the array, thus passing a pointer to the array, not a reference to it. But there is nofunc(char(*)[len])
function defined, so this is also an error.To pass
str
by reference, you need to simply passstr
as-is without*
or&
:func(str);
This is no different than passing a reference to a variable of any other type, eg:
On a side note:
printf(str);
is dangerous, since you don't know ifstr
contains any%
characters in it. A safer call would be either:printf("%s", str);
Or:
puts(str);
But those only work if
str
is null-terminated (which it is in your case). Even safer would be:printf("%.s", (int)len, str);
Which doesn't require a null terminator.