初始化由指针传递的数组
该函数无法初始化数组,因为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,不,没有其他问题。您所说的问题是阻止您初始化数组的唯一问题。
通常,只需传递大小和指针即可解决此问题:
请注意,我使用
std::size_t
作为大小,因为这是存储大小的标准类型(它将是 8 个字节) 64 位机器,而int
通常不是)。在某些情况下,如果大小是静态已知的,则可以使用模板:
但是,这仅适用于数组,不适用于指向已分配数组的指针。
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:
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, whereasint
usually isn't).In some cases, if the size is known statically, then you can use a template:
However, this only works with arrays, not pointers to allocated arrays.
我没有看到其他问题。但是,您可能想要这样:
当然,除非即使编译器在编译时也不知道它有多大。在这种情况下,您必须显式传递尺寸。
如果您不知道尺寸,则说明存在设计错误。
http://codepad.org/Sj2D6uWz
I don't see other problems. However, you probably wanted this:
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.
If you don't know the size, you have a design error.
http://codepad.org/Sj2D6uWz
您应该能够区分两种类型的数组。如下所示:
该数组的类型为
type[count]
,每个count
的类型不同。虽然它可以转换为type *
,但它是不同的。一个区别是sizeof(name)
为您提供count*sizeof(type)
另一种类型的数组如下所示:
这基本上只是一个指针,可以使用数组进行初始化,例如使用
malloc
或new
。该变量的类型为type *
,正如您所看到的,该类型中没有count个信息。因此,sizeof(name)
为您提供计算机中指针的大小,例如 4 或 8 字节。您可能会问,为什么这两个
sizeof
不同?因为sizeof
是在编译时计算的。考虑以下代码:现在,当您说
sizeof(name)
时,编译器无法知道n
未来可能的值。因此,它无法将sizeof(name)
计算为数组的实际大小。此外,name
指针甚至可能不指向数组!你问,你应该做什么?简单的。将数组的大小保存在一个变量中,并将其拖动到您获取数组的位置。所以在你的情况下它会是这样的:
There are two types of arrays you should be able to distinguish. One looks like this:
This array is of type
type[count]
which is a different type for eachcount
. Although it is convertable totype *
, it is different. One difference is thatsizeof(name)
gives youcount*sizeof(type)
The other type of array looks like this:
Which is basically just a pointer that you could initialize with an array for example with
malloc
ornew
. The type of this variable istype *
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
sizeof
s different, you ask? Becausesizeof
is evaluated at compile time. Consider the following code:Now, when you say
sizeof(name)
, the compiler can't know the possible future value ofn
. Therefore, it can't computesizeof(name)
as the real size of the array. Besides, thename
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: