C sizeof char 指针 [英] C sizeof char pointer

查看:27
本文介绍了C sizeof char 指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个 char 变量的大小等于 1?

int main(){char s1[] = "你好";fprintf(stderr, "(*s1) : %i
", sizeof(*s1) )//打印出 1}

解决方案

NOTA: 原来的问题一开始有点变化是:为什么这个char指针的大小是1p>

sizeof(*s1)

一样

sizeof(s1[0])char 对象的大小,而不是 char 指针的大小.

char 类型对象的大小在 C 中始终为 1.

要获取 char 指针的大小,请使用以下表达式:sizeof (&s1[0])

Why is size of this char variable equal 1?

int main(){

char s1[] = "hello";

fprintf(stderr, "(*s1) : %i
", sizeof(*s1) )    // prints out 1

}

解决方案

NOTA: the original question has changed a little bit at first it was: why is the size of this char pointer 1

sizeof(*s1)

is the same as

sizeof(s1[0]) which is the size of a char object and not the size of a char pointer.

The size of an object of type char is always 1 in C.

To get the size of the char pointer use this expression: sizeof (&s1[0])

这篇关于C sizeof char 指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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