首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

指针仍是数组

2012-12-31 
指针还是数组有这样一个现象:char* ss 0123456789sizeof(ss)//结果 4 ===》ss是指向字符串常量的字符

指针还是数组
有这样一个现象:

char* ss = "0123456789";
sizeof(ss)  //结果 4 ===》ss是指向字符串常量的字符指针
sizeof(*ss) //结果 1 ===》*ss是第一个字符

char ss[] = "0123456789";
sizeof(ss)  //结果 11 ===》ss是数组,计算到\0位置,因此是10+1
sizeof(*ss) //结果 1 ===》*ss是第一个字符

问题来了:1.数组的名称不就是指向数组首元素的指针吗?那么这两种表示形式有什么区别呢,为什么sizeof一个计算指针的内存占用,一个计算数组的内存占用。 
2.如果用第一种形式定义,如何使用sizeof(不用strlen())来计算字符创的长度? 
 
先谢谢各位了!
[解决办法]
《C++ primer》------ 当 sizeof 操作符应用在数组上时,它返回整个数组的字节长度
[解决办法]
数组和指针不一样的。。指针只是存放地址的一个变量,而且在大部分情况下sizeof(指针)的值都是4。
[解决办法]
这算是数组名 和 指针的一个区别
[解决办法]
If the type of an expression or subexpression is ``array of T,'' for some type T, then the value of the expression is a pointer to the first object in the array, and the type of the expression is altered to ``pointer to T.'' This conversion does not take place if the expression is in the operand of the unary & operator, or of ++, --, sizeof, or as the left operand of an assignment operator or the . operator. Similarly, an expression of type ``function returning T,'' except when used as the operand of the & operator, is converted to ``pointer to function returning T.''

热点排行