数组不是指针,对吧? [英] Arrays are not Pointers, right?

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

问题描述

我一直都教过数组不是指针。那我该怎么办:

#include< iostream>

#include< cstdlib>


using namespace std ;


int main()

{

int myArray [10] = {1,10};

cout<< sizeof(myArray)<< endl;

cout<< sizeof(myArray + 1)<< endl;

cout<< * myArray<< endl;

cout<< *(myArray + 1)<<结束;


系统(暂停);

返回0;

}

/ *结果是

40

4

1

10

* /

-

Gary

I''ve always taught that arrays are not pointers. So how come I can do:
#include <iostream>
#include <cstdlib>

using namespace std;

int main( )
{
int myArray[10]={1, 10};
cout << sizeof(myArray) << endl;
cout << sizeof(myArray+1) << endl;
cout << *myArray << endl;
cout << *(myArray+1) << endl;

system("Pause");
return 0;
}
/*results are
40
4
1
10
*/
--
Gary

推荐答案



"加里" < GL ******* @ comcast.net>在消息中写道

新闻:Z9 ******************** @ comcast.com ...

"Gary" <gl*******@comcast.net> wrote in message
news:Z9********************@comcast.com...
我我总是教导数组不是指针。那我该怎么做:
#include< iostream>
#include< cstdlib>

使用命名空间std;

int main( )
{
int myArray [10] = {1,10};
cout<< sizeof(myArray)<< endl;
cout<< sizeof(myArray + 1)<< endl;
cout<< * myArray<< endl;
cout<< *(myArray + 1)<< endl;

系统(暂停);
返回0;
}
/ *结果是
40
4 /> 1
10
* /
I''ve always taught that arrays are not pointers. So how come I can do:
#include <iostream>
#include <cstdlib>

using namespace std;

int main( )
{
int myArray[10]={1, 10};
cout << sizeof(myArray) << endl;
cout << sizeof(myArray+1) << endl;
cout << *myArray << endl;
cout << *(myArray+1) << endl;

system("Pause");
return 0;
}
/*results are
40
4
1
10
*/



数组指针不相同但相互关联。

arr [i] = = *(arr + i)

arr是数组起始元素的地址。

arr + 1是数组第二个元素的地址。 br />
* arr是第一个元素的值

*(arr + 1)是数组的第二个元素。

简单:-)


-Sharad


Arrays are pointers are not same but inter-related.
arr[i] == *(arr+i)
arr is the address of the starting element of the array.
arr+1 is the address of the 2nd element of the array.
*arr is the value of the 1st element
*(arr+1) is the 2nd element of the array.
Simple :-)

-Sharad


Gary写道:
我一直都教过数组不是指针。那我该怎么办:
I''ve always taught that arrays are not pointers. So how come I can do:



[SNIP]


因为在某些表达式中,数组_decays_的名称变为指针

到数组的第一个元素。


-

Attila又名WW


[SNIP]

Because in certain expressions the name of the array _decays_ into a pointer
to the first element of the array.

--
Attila aka WW




" Attila Feher" <在********** @ lmf.ericsson.se>在消息中写道

news:bv ********** @ newstree.wise.edt.ericsson.se ...

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bv**********@newstree.wise.edt.ericsson.se...
Gary写道:
我一直都教过数组不是指针。那我该怎么办:[SNIP]

因为在某些表达式中,数组_decays_的名称变为
I''ve always taught that arrays are not pointers. So how come I can do: [SNIP]

Because in certain expressions the name of the array _decays_ into a



指向数组第一个元素的指针。


pointer to the first element of the array.




是的,但我很惊讶我可以对野兽进行指针运算。

sizeof(数组+ 1)回来了。我原本以为它是整个数组第二个字节的地址

。哦,好吧。

-

加里



Yeah, but I was surprised that I could do pointer arithmetic on the beast.
sizeof(array+1) came back okay. I would have thought it would be the address
of the second byte of the entire array. Oh, well.
--
Gary


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

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