sizeof(&array) 返回什么? [英] What does sizeof(&array) return?

查看:32
本文介绍了sizeof(&array) 返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照问题:如何数组的地址是否等于它在 C 中的值?

#include <stdio.h>
#define N 10    
char str2[N]={"Hello"};
int main(){
    printf("sizeof(str2): %d bytes
", sizeof(str2));
    printf("sizeof(&str2): %d bytes
", sizeof(&str2));
    return 0;
}

输出:

sizeof(str2): 10 bytes
sizeof(&str2): 4 bytes

我知道只有 str2 是数组 str2 中第一个元素的地址.当 str2sizeof 的参数时,它返回整个数组 str2 的大小.

I know that str2 alone is the address of the first element in array str2. And that when str2 is an argument of sizeof it returns the size of the whole array str2.

另外,&str2 也是 arr str2 中第一个元素的地址,只是类型不同 (char (*)[N] == 指向数组的指针).但是当 &str2sizeof 的参数时,它的表现如何?

In addition, &str2 is also the address of the first element in arr str2 but from different type (char (*)[N] == pointer to array). But how does &str2 behave when it's an argument of sizeof?

推荐答案

&str2 是一个指针.所以你只是在你的平台上看到一个指针的大小.

&str2 is a pointer. So you're just seeing the sizeof a pointer on your platform.

这篇关于sizeof(&amp;array) 返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆