8.3 指针与数组(*pointer+number或pointer[number])

Posted: March 26, 2011 in C++

/*其实,数组名可以看作是 常量 的指针名.

比如: int number[3]={1,2,3,4};
number[2]指的就是3.

这里主要演示指针与数组之间的用法.
*/

//可以使用*pointer+number或pointer[number]的方法调取数组的值
#include <iostream>
#include <string>

using namespace std;

void main()
{
            string Tong[3]={“Ethan”,“Rickson”,“Owen”}; //Tong家有3个人:Ethan,Rickson,Owen
            string *pointer=Tong;//门牌号记住Tong家的3个人

            cout<<*(pointer+0)<<endl;//叫老大Ethan出来
            cout<<pointer[1]<<endl;//叫老二Rickson出来
            cout<<pointer[2]<<endl;//叫老三Owen出来
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s