Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
正确调用函数传递
a
及其大小的正确语法将如下所示。当将a
传递给该功能时,无需让Square Brackets[]
。此外,要将数组的大小作为第二个参数,您可以使用
std :: size
,它可以使用c ++ 17
使用,也可以使用Expressiona / sizeof a [0] < / code>的尺寸也如下所示。
或
工作demo
The correct syntax to call the function passing
a
and its size would be as shown below. There is no need to have the square brackets[]
when passinga
to the function.Additionally, for passing the size of the array as the second argument, you can either use
std::size
which is available withC++17
or use the expressionsizeof a / sizeof a[0]
also shown below.Or
Working demo
当您将数组作为参数传递到函数中时,您只需将其传递,就好像它是变量一样。在这种情况下,只有
a
就可以做到。这是因为 arrays”> arrays“ decay'to pointers so代码>是您数组的“指针”。
此外,我建议除
sizeof(a [0])
以 sizeof函数返回字节中的大小When you pass an array as an argument into a function, you simply pass it as if it were a variable. In this case, simply just
a
would do. This is because arrays "decay" into pointers soa
is a "pointer" to your array.In addition, I recommend dividing by the
sizeof(a[0])
to get the full length as the sizeof function returns the size in bytes您可以传递对数组的引用,而不是通过指针和长度。
您也不需要
[]
即可为a
;Rather than pass a pointer and a length, you could pass a reference to the array.
You also don't need
[]
to namea
;