c++: the hint of pointer

Okay.

Today i get a new concept about pointer:

I usually treat pointer as a tag of address. But now I think i made some mistakes.

First of all: when a pointer  point to the the first of address (eg. array ),  it obviously shows that the property of "index" or "offset". Its behavior represents you tell the hardware where the base address is, then when you try to operate it. the typical operation just likes the “add" and "minus" ,  it means the hardware should justice the next action, drift action show immediately.  

Right now i know GNU C++ compiler which have this behaviors:

for instance:

// tacos[8]:: index(8) - tacos[1]::index(1) = 8 - 1 = 7, the result is 7....

# include <iostream>

int main()
{
 using namespace std;
 int tacos[10] = {5,2,8,4,1,2,2,4,6,8};
 int  * pt  = tacos;
 cout << "Pt's address: " << pt << endl;
 pt = pt + 1;
 cout << "Pt's address: ”  << pt  << " and " <<  * pt << endl;
 int * pe = & tacos[9];
 pe = pe -1;  // tacos[8]::index - tacos[1]::index = 1
 int diff = pe - pt;  
 cout << "pt's value: " << *pt << endl;  
 cout << "pe's value: " << *pe << endl; 
 cout << "diff's value: " << diff << endl;  
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值