C++ 中的 sizeof() 运算符 [已关闭]

问题描述 投票:0回答:1

为什么在 C++ 中我们要写 sizeof(x) / sizeof(x[0]) 而不是简单地写 sizeof(x) 来确定数组的大小

如果我们只写 sizeof(x) 来查找数组的大小,我希望它能给出正确的输出。

c++ sizeof
1个回答
1
投票

因为

sizeof
使用字节单位,而不是对象。如今,对于数组,最好使用
std::extent
来获取数组维度(链接页面包含示例)。另外,当参数是对象时,不需要括号,因此
sizeof x / sizeof x[0]
就足够了。类型需要括号。

© www.soinside.com 2019 - 2024. All rights reserved.