*(指针+索引)和指针之间的差异[] [英] Difference Between *(Pointer + Index) and Pointer[]

查看:1424
本文介绍了*(指针+索引)和指针之间的差异[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int * myPointer = new int [100]; 

// ...

int firstValue = *(myPointer + 0);
int secondValue = myPointer [1];

*(myPointer + index) myPointer [index]


b b b b b b b p>语义上,指针解引用说这是一件事,但我真的关心 X 空格,而数组访问说这里有一堆东西,我关心 Xth 一个。



在大多数情况下,我更喜欢数组形式。 >

int* myPointer = new int[100];

// ...

int firstValue = *(myPointer + 0);
int secondValue = myPointer[1];

Is there any functional difference between *(myPointer + index) and myPointer[index]? Which is considered better practice?

解决方案

Functionally, they are identical.

Semantically, the pointer dereference says "Here's a thing, but I really care about the thing X spaces over", while the array access says "Here's a bunch of things, I care about the Xth one."

In most cases, I would prefer the array form.

这篇关于*(指针+索引)和指针之间的差异[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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