C++ 上的 Sizeof()数组在一个函数中起作用,但在另一个函数中不起作用

发布于 2024-11-23 15:33:35 字数 735 浏览 4 评论 0原文

由于我是编程新手,我正在尝试了解有关数组的更多信息。所以我正在研究代码的不同部分,并试图了解三件事: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 技术交流群。

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

发布评论

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

评论(3

凤舞天涯 2024-11-30 15:33:35

void arrayprint(int inarray[])

相当于:

void arrayprint(int *inarray)

所以你得到了机器上 int 指针的大小。即使您的问题是关于 C++ 并且您的函数看起来有点不同,它也可以归结为 C FAQ< /a> 条目。

所以,在 main 内部 n_array 实际上是一个真正诚实的数组。但是当传递给函数时,它会“衰减”为指针。而且没有办法知道实际尺寸。您所能做的就是向您的函数传递额外的参数。

void arrayprint(int inarray[], int len)
{
    for (int n_i = 0 ; n_i < len; n_i++)
    /* .... */

并这样称呼它:

arrayprint(n_array, sizeof(n_array) / sizeof(n_array[0]));

当然,真正的解决方案是使用向量,因为您使用的是 C++。但我对C++了解不多。

This

void arrayprint(int inarray[])

Is equivalent to this:

void arrayprint(int *inarray)

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.

void arrayprint(int inarray[], int len)
{
    for (int n_i = 0 ; n_i < len; n_i++)
    /* .... */

And call it like this:

arrayprint(n_array, sizeof(n_array) / sizeof(n_array[0]));

Of course, the real solution would be to use vectors since you're using C++. But I wouldn't know much about C++.

一向肩并 2024-11-30 15:33:35

您遇到的是数组到指针的转换,这是 C++ 继承的 C 语言功能。有一篇关于此的详细 C++-faq 帖子: How do I use arrays in C++?

由于您使用的是 C++,因此您可以通过引用传递数组:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
        cout << inarray[n_i] << endl;
}

尽管在这种情况下,sizeof 计算是多余的:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (size_t n_i = 0 ; n_i < N ; n_i++)
        cout << inarray[n_i] << '\n';
}

而且,正如已经提到的,如果您的目标不是专门将 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:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
        cout << inarray[n_i] << endl;
}

although in that case the sizeof calculation is redundant:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (size_t n_i = 0 ; n_i < N ; n_i++)
        cout << inarray[n_i] << '\n';
}

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.

莫言歌 2024-11-30 15:33:35

main() 中,您的 n_array 是一个数组。当您使用 sizeof(n_array) 时,您将获得数组的大小(以字节为单位)。准确地说,您获得的大小使得 sizeof(char) 等于 1。在当今大多数现代计算机上,这是一个字节。

数组不是指针。但是,当您将数组传递给某个函数时,传递的是数组第零个元素的地址。数组退化为指针。因此,您的 arrayprint() 的原型最好是 void arrayprint(int* inarray)。当您使用 sizeof(inarray) 时,您并不是在 main() 中计算 n_array 的大小。您正在计算指针的大小。

In main() your n_array is an array. When you take sizeof(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 as void arrayprint(int* inarray). When you take sizeof(inarray) you aren't computing the size of n_array in main(). You are computing the size of a pointer.

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