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.
Xth
but the X+1th
.
Jan 12, 2011 at 22:17
*(p + offset)
whenever I have a pointer, for some idea of consistency (vs 'actual' arrays), but in practical terms I find p[offset]
looks better and more intuitive in most cases, relegating the +
form to 'offset from this other thing'.
Jan 16, 2016 at 11:40
There is no difference between
*(array+10); //and
array[10];
but guess what? since +
is commutative
*(10 + array); //is all the same
10[array]; //! it's true try it !
+
is commutative. Operation +
is also associative, but this isn't important here.
No, they are functionally equivalent.
First, index
is scaled up to the type size then added to the myPointer
base, then the value is extracted from that memory location.
The "better practice" is the more readable one, which is usually, but not necessarily always, the myPointer[index]
variant.
That's because you're usually interested in an element of the array, not the memory location to dereference.
There is no functional difference I know of but the form myPointer[1]
is ultimately more readable and far less likely to incur coding errors.
DC
The form *(myPointer + 1)
does not allow for changing the type of pointer to an object and therefore getting access to the overloaded [] operator.
Also debugging is far harder
int *ints[10];
int myint = ints[10];
is easier to pickup visually than
int *ints;
int myint = *(ints + 10);
also the compiler can insert range checking to catch the error at compile time.
DC
ints + 10
is better than &ints[10]
.
More readable and more maintainable code is better code.
As for functional part... There is no difference. Both times you are "playing with memory".
There is no functional difference. The decision to use either form is usually made depending on the context in which you are using it. Now in this example, the array form is simpler to use and read and hence is the obvious choice. However, suppose you were processing a character array, say, consuming the words in a sentence. Given a pointer to the array you might find it easier to use the second form as in the code snippet below:
int parse_line(char* line)
{
char* p = line;
while(*p)
{
// consume
p++;
}
...
}
Edit 1 : Decade-old question. But still, I think this answer will help to know the compiler's perspective.
Compiler creates the same machine code for both cases. here's a proof,
code 1
#include<stdio.h>
int main()
{
int myArr[5] = {1, 2, 3, 4, 5};
int value = myArr[0];
}
code 2
#include<stdio.h>
int main()
{
int myArr[5] = {1, 2, 3, 4, 5};
int value = *(myArr + 0);
}
Below is the result of the comparison done on assembly code generated by compiling the C code of both the codes with gcc -S.
Actually , When an Array 'a' is initialized a pointer to its first memory location ie.. a[0] is returned which is nothing but a ;
So if you do 'a+1' it is actually a pointer to a[1]
if you do 'a+2' it is actually a pointer to a[2]
if you do 'a+3' it is actually a pointer to a[3] so on ,
so if you do *(a+1) you will get value of a[1] and similar for other values also. if you do *(a) you actually get a[0], So i think its pretty clear now how it works..
int thirdValue = 2[myPointer];
Which crazily also works.array[index]
andindex[array]
are just syntax sugar for*(array + index)
.