初始化由指针传递的数组

发布于 2024-12-11 00:45:57 字数 269 浏览 0 评论 0原文

该函数无法初始化数组,因为 sizeof() 返回 int 指针 的字节 不是 myArray 指向的内存大小。

 void assignArray(int *myArray)
 {
     for(int k = 0; k < sizeof(myArray); ++k)
     {
         myArray[k] = k;
     }
 }

还有其他问题吗?

谢谢

The function cannot initialize an array because sizeof() returns bytes of an int pointer
not the size the memory pointed by myArray.

 void assignArray(int *myArray)
 {
     for(int k = 0; k < sizeof(myArray); ++k)
     {
         myArray[k] = k;
     }
 }

Are there other problems ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

墨离汐 2024-12-18 00:45:57

嗯,不,没有其他问题。您所说的问题是阻止您初始化数组的唯一问题。

通常,只需传递大小和指针即可解决此问题:

void assignArray(int* myArray, std::size_t mySize)
{
    for (std::size_t k = 0; k < mySize; ++k)
        myArray[k] = k;
}

请注意,我使用 std::size_t 作为大小,因为这是存储大小的标准类型(它将是 8 个字节) 64 位机器,而 int 通常不是)。

在某些情况下,如果大小是静态已知的,则可以使用模板:

template <std::size_t Size>
void assignArray(int (&myArray)[Size])
{
    for (std::size_t k = 0; k < Size; ++k)
        myArray[k] = k;
}

但是,这仅适用于数组,不适用于指向已分配数组的指针。

int array1[1000];
int* array2 = new int[1000];
assignArray(array1); // works
assignArray(array2); // error

Well no, there are no other problems. The problem you stated is the only thing stopping you from initialising the array.

Typically, this is solved by simply passing the size along with the pointer:

void assignArray(int* myArray, std::size_t mySize)
{
    for (std::size_t k = 0; k < mySize; ++k)
        myArray[k] = k;
}

Note that I've used std::size_t for the size because that is the standard type for storing sizes (it will be 8 bytes of 64-bit machines, whereas int usually isn't).

In some cases, if the size is known statically, then you can use a template:

template <std::size_t Size>
void assignArray(int (&myArray)[Size])
{
    for (std::size_t k = 0; k < Size; ++k)
        myArray[k] = k;
}

However, this only works with arrays, not pointers to allocated arrays.

int array1[1000];
int* array2 = new int[1000];
assignArray(array1); // works
assignArray(array2); // error
或十年 2024-12-18 00:45:57

我没有看到其他问题。但是,您可能想要这样:

template<int sz>
void assignArray(int (&myArray)[sz])
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

当然,除非即使编译器在编译时也不知道它有多大。在这种情况下,您必须显式传递尺寸。

void assignArray(int* myArray, size_t sz)
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

如果不知道尺寸,则说明存在设计错误。

http://codepad.org/Sj2D6uWz

I don't see other problems. However, you probably wanted this:

template<int sz>
void assignArray(int (&myArray)[sz])
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

Unless, of course, even the compiler doens't know how big it is at compile time. In which case you have to pass a size explicitly.

void assignArray(int* myArray, size_t sz)
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

If you don't know the size, you have a design error.

http://codepad.org/Sj2D6uWz

秋风の叶未落 2024-12-18 00:45:57

您应该能够区分两种类型的数组。如下所示:

type name[count];

该数组的类型为 type[count],每个 count 的类型不同。虽然它可以转换为type *,但它是不同的。一个区别是 sizeof(name) 为您提供 count*sizeof(type)

另一种类型的数组如下所示:

type *name;

这基本上只是一个指针,可以使用数组进行初始化,例如使用mallocnew。该变量的类型为type *,正如您所看到的,该类型中没有count个信息。因此,sizeof(name) 为您提供计算机中指针的大小,例如 4 或 8 字节。

您可能会问,为什么这两个 sizeof 不同?因为 sizeof 是在编译时计算的。考虑以下代码:

int n;
cin >> n;
type *name = new type[n];

现在,当您说 sizeof(name) 时,编译器无法知道 n 未来可能的值。因此,它无法将 sizeof(name) 计算为数组的实际大小。此外,name 指针甚至可能不指向数组!

你问,你应该做什么?简单的。将数组的大小保存在一个变量中,并将其拖动到您获取数组的位置。所以在你的情况下它会是这样的:

void assignArray(int *myArray, int size)
{
    for(int k = 0; k < size; ++k)
    {
        myArray[k] = k;
    }
}

There are two types of arrays you should be able to distinguish. One looks like this:

type name[count];

This array is of type type[count] which is a different type for each count. Although it is convertable to type *, it is different. One difference is that sizeof(name) gives you count*sizeof(type)

The other type of array looks like this:

type *name;

Which is basically just a pointer that you could initialize with an array for example with malloc or new. The type of this variable is type * and as you can see, there are no count informations in the type. Therefore, sizeof(name) gives you the size of a pointer in your computer, for example 4 or 8 bytes.

Why are these two sizeofs different, you ask? Because sizeof is evaluated at compile time. Consider the following code:

int n;
cin >> n;
type *name = new type[n];

Now, when you say sizeof(name), the compiler can't know the possible future value of n. Therefore, it can't compute sizeof(name) as the real size of the array. Besides, the name pointer might not even point to an array!

What should you do, you ask? Simple. Keep the size of the array in a variable and drag it around where ever you take the array. So in your case it would be like this:

void assignArray(int *myArray, int size)
{
    for(int k = 0; k < size; ++k)
    {
        myArray[k] = k;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文