返回信息流程序如下:
#include <iostream.h>
int main()
{
int str[3]={1,2,3};
int (*p)[3]=&str;
cout << "*str is " << *str <<endl;
cout << "str is " << str <<endl;
cout << "*p is " << *p <<endl;
cout << "p is " << p <<endl;
return 0;
}
输出的是:
*str is 1;
str is 0x0012FF3C
*p is 0x0012FF3C
p is 0x0012FF3C
前两个明白~后面两个不明白~~~[ema16]
这是一条镜像帖。来源:北邮人论坛 / cpp / #42779同步于 2010/8/27
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【求助】被指针数组弄晕了~
jingyao
2010/8/27镜像同步24 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
str+0, str, &str在数值上是相等的,只是类型不同
str+0的类型是 int *
str的类型是int [3]
&str的类型是int (*)[3]
【 在 jingyao (吱吱~) 的大作中提到: 】
: 程序如下:
: #include <iostream.h>
: int main()
: {
: int str[3]={1,2,3};
: int (*p)[3]=&str;
: cout << "*str is " << *str <<endl;
: cout << "str is " << str <<endl;
: cout << "*p is " << *p <<endl;
~~~~ *p就是str,
: cout << "p is " << p <<endl;
~~~~~p是指针,在数值上等于str
: return 0;
: }
: 输出的是:
: *str is 1;
: str is 0x0012FF3C
: *p is 0x0012FF3C
: p is 0x0012FF3C
: 前两个明白~后面两个不明白~~~[ema16]
【 在 wildpointer 的大作中提到: 】
: str+0, str, &str在数值上是相等的,只是类型不同
: str+0的类型是 int *
: str的类型是int [3]
: ...................
还是~~~
想问一下:
#include <iostream.h>
int count[5]={1,2};
void main()
{
int i;
char c[ ]="hello!";
cout<<c<<endl;
cout<<count<<endl;
}
输出为:
hello! //输出字符串c
0x00408040 //数组count首地址
为什么他不把char c[]也看成数组……难道要char c[]={h,e,l,l,o,!}才是吗?
谢谢哈~~比较笨[ema13]
cout << c;
当数组当参数的时候,退化成了指向数组首元素的指针。
输出char *,就是当字符串输出。
用char c[] = {'h', 'e', 'l', 'l' ,'o', '!'};也不行,因为c当参数时,还是char *。而且你用这种方法输出c,后面很可能还有乱码,因为它一直输出,直到遇到'\0'。
如果你非要输出c的首地址,就强制转换一下cout << hex << (unsigned int) c << endl;
如果你在C语言中想得到字符数组c的地址:printf("%p", c);
【 在 jingyao (吱吱~) 的大作中提到: 】
: 还是~~~
: 想问一下:
: #include <iostream.h>
: int count[5]={1,2};
: void main()
: {
: int i;
: char c[ ]="hello!";
: cout<<c<<endl;
: cout<<count<<endl;
: }
: 输出为:
: hello! //输出字符串c
: 0x00408040 //数组count首地址
: 为什么他不把char c[]也看成数组……难道要char c[]={h,e,l,l,o,!}才是吗?
: 谢谢哈~~比较笨[ema13]
【 在 jingyao 的大作中提到: 】
: 程序如下:
: #include <iostream.h>
: int main()
: ...................
附件(237.3KB)
【 在 hpttlook 的大作中提到: 】
: : 程序如下:
: : #include <iostream.h>
: : int main()
: ...................
谢谢哈O(∩_∩)O~
有点儿明白了~~
我要顶一下你的附件。
我有《再再论指针》的修订版。我从中学到了不少东西。
【 在 hpttlook 的大作中提到: 】
: : 程序如下:
: : #include <iostream.h>
: : int main()
: ...................
附件(247.9KB)
【 在 wildpointer 的大作中提到: 】
: 我要顶一下你的附件。
: 我有《再再论指针》的修订版。我从中学到了不少东西。
: 【 在 hpttlook 的大作中提到: 】
: ...................
以前也是一直不清楚,很多中文教材上,还是当时老师讲的,确实就没清楚过,有的甚至就是错的。
觉得刚开始学的时候,就把这些就弄清楚比较好~