C++ 上的 Sizeof()数组在一个函数中起作用,但在另一个函数中不起作用
由于我是编程新手,我正在尝试了解有关数组的更多信息。所以我正在研究代码的不同部分,并试图了解三件事:sizeof()
。如何找到数组的长度,以及如何将数组放入函数中(作为参数)?
我的代码是:
#include <iostream>
using namespace std;
void arrayprint(int inarray[])
{
for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
{
cout << inarray[n_i] << endl;
}
}
int main()
{
int n_array[5];
for (int n_i = 0 ; n_i < (sizeof(n_array) / sizeof(int)) ; n_i++ )
{
cin >> n_array[n_i];
}
arrayprint(n_array);
return 0;
}
看来 sizeof(inarray) / sizeof(int)
在 main 函数中起作用,但在 arrayprint 函数中不起作用。在数组 print 函数中,它的计算结果为 1,而在 main 函数中,它的计算结果为 5。为什么?
所以我想我想知道的是它们有何不同?为什么?
I'm trying to learn more about arrays since I'm new to programming. So I was playing with different parts of code and was trying to learn about three things, sizeof()
. How do I find the length of an array, and also how do I put arrays into functions (as a parameter)?
My code is:
#include <iostream>
using namespace std;
void arrayprint(int inarray[])
{
for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
{
cout << inarray[n_i] << endl;
}
}
int main()
{
int n_array[5];
for (int n_i = 0 ; n_i < (sizeof(n_array) / sizeof(int)) ; n_i++ )
{
cin >> n_array[n_i];
}
arrayprint(n_array);
return 0;
}
It seems the sizeof(inarray) / sizeof(int)
works in the main function, but not in the arrayprint function. In the array print function, it evaluates to 1, while in main it evaluates to 5. Why?
So I guess what I want to know is how they are different? And why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这
相当于:
所以你得到了机器上 int 指针的大小。即使您的问题是关于 C++ 并且您的函数看起来有点不同,它也可以归结为 C FAQ< /a> 条目。
所以,在
main
内部n_array
实际上是一个真正诚实的数组。但是当传递给函数时,它会“衰减”为指针。而且没有办法知道实际尺寸。您所能做的就是向您的函数传递额外的参数。并这样称呼它:
当然,真正的解决方案是使用
向量
,因为您使用的是 C++。但我对C++了解不多。This
Is equivalent to this:
So you are getting the size of an int pointer on your machine. Even if your question is about C++ and your function looks a bit different, it boils down to this C FAQ entry.
So, inside
main
n_array
is actually a true honest array. But when passed to a function, it "decays" into a pointer. And there's no way to know the real size. All you can do is pass additional arguments to your function.And call it like this:
Of course, the real solution would be to use
vector
s since you're using C++. But I wouldn't know much about C++.您遇到的是数组到指针的转换,这是 C++ 继承的 C 语言功能。有一篇关于此的详细 C++-faq 帖子: How do I use arrays in C++?
由于您使用的是 C++,因此您可以通过引用传递数组:
尽管在这种情况下,sizeof 计算是多余的:
而且,正如已经提到的,如果您的目标不是专门将 C 数组传递给函数,但要通过一个容器或值序列,请考虑使用 std::vector 和其他容器和/或迭代器。
What you've encountered was array-to-pointer conversion, a C language feature inherited by C++. There's a detailed C++-faq post about this: How do I use arrays in C++?
Since you're using C++, you could pass the array by reference:
although in that case the sizeof calculation is redundant:
And, as already mentioned, if your goal is not specifically to pass a C array to a function, but to pass a container or a sequence of values, consider using
std::vector
and other containers and/or iterators.在
main()
中,您的n_array
是一个数组。当您使用 sizeof(n_array) 时,您将获得数组的大小(以字节为单位)。准确地说,您获得的大小使得 sizeof(char) 等于 1。在当今大多数现代计算机上,这是一个字节。数组不是指针。但是,当您将数组传递给某个函数时,传递的是数组第零个元素的地址。数组退化为指针。因此,您的
arrayprint()
的原型最好是void arrayprint(int* inarray)
。当您使用sizeof(inarray)
时,您并不是在main()
中计算n_array
的大小。您正在计算指针的大小。In
main()
yourn_array
is an array. When you takesizeof(n_array)
you are getting the size of the array, in bytes. To be precise, you are getting the size such that sizeof(char) is identically equal to one. On most modern computers nowadays that is a byte.An array is not a pointer. However, when you pass an array to some function what is passed is the address of the zeroth element of the array. The array degrades into a pointer. So your
arrayprint()
would be better prototyped asvoid arrayprint(int* inarray)
. When you takesizeof(inarray)
you aren't computing the size ofn_array
inmain()
. You are computing the size of a pointer.