gpt4 book ai didi

c++ - sizeof() 的参数

转载 作者:太空宇宙 更新时间:2023-11-04 14:48:46 25 4
gpt4 key购买 nike

for大小的输出

#include<iostream>
using namespace std;
struct node
{
int k;
struct node *next;
};
int main()
{
int arr[3];
cout<<sizeof(struct node)<<endl;
cout<<sizeof(struct node *)<<endl;
cout<<sizeof(arr)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<sizeof(int *)<<endl;
return 0;
}

8
4
12
4
4

我理解 struct node * 是一个指针,所以它的输出应该是 4。所以类似 arr 也是一个指针,所以它的输出也应该是 4 但为什么它显示 arr 的大小数组为 12?

最佳答案

So similarly arr is also a pointer

不,arr 不是一个指针,它是一个数组。虽然它可以自由转换为指针,但它是另一种对象。 sizeof 的结果表示存储指定大小的数组所需的内存量,即 int 大小的三倍。

关于c++ - sizeof() 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com