C++:通过参考传递数组

发布于 2025-01-18 12:21:32 字数 1411 浏览 2 评论 0原文

我正在尝试定义一个函数原型,它采用不同长度的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂人 2025-01-25 12:21:32

您的功能已正确声明,但是您没有正确地将数组传递给它。

func(*str);首先将数组衰减到指向第一个元素的指针,然后延迟指针,因此将第一个字符传递到func()。但是没有定义func(char)函数,因此这是一个错误。

func(&amp; str);占据了数组的地址,因此将指针传递给数组,而不是对其的引用。但是没有定义的func(char(*)[len])函数,因此这也是一个错误。

要通过参考传递str,您只需通过str as-as-as-is-is-is-i *&amp;

<代码> func(str);

这与对任何其他类型的变量的引用,例如:

void func(int &value);

int i;
func(i);

在侧面注: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 to func(). But there is no func(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 no func(char(*)[len]) function defined, so this is also an error.

To pass str by reference, you need to simply pass str as-is without * or &:

func(str);

This is no different than passing a reference to a variable of any other type, eg:

void func(int &value);

int i;
func(i);

On a side note: printf(str); is dangerous, since you don't know if str 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文