本文介绍了用C的sizeof()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("\n %d %d",sizeof(a),sizeof(b));
printf("\n %d %d",sizeof(*a),sizeof(*b));
}
的sizeof(A)
给我的输出: 11
(即字符串的长度)
sizeof(a)
gives me output: 11
( that is length of the string)
为什么会这样呢?结果
为什么不是输出的sizeof(A)
= 4
,因为当我尝试打印一个
它给了我一个地址值,因此一个整数?
Why is it so ?
Why isn't the output sizeof(a)
=4
since when I try to print a
it gives me an address value and hence an integer?
推荐答案
当你是指在程序中数组的名称,它通常衰减到一个指向数组的第一个元素。其中一个例外的是的sizeof
运营商。所以,当你考虑以下code。
Whenever you refer to the name of the array in your program, It normally decays to a pointer to the first element of the array. One of the exception to this rule is the sizeof
operator. So when you consider the following code.
int main()
{
char a[] = "Visual C++";
printf("sizeof(a)=%d\n",sizeof(a)); /* Here sizeof(a) indicates sizeof array */
printf("a=%p",a); /* Here the array name, passed as an argument to printf decays into a pointer of type (char *) */
return 0;
}
这篇关于用C的sizeof()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!