# Thread: Pointer and Arrays

1. ## Pointer and Arrays

Can anyone tell me why you would have four different ways to access arrays ?
[1] Array subscript notation.
[2] pointer offset notation using nPtr and offset.
[3] pointer offset notation using array name and offset
[4] using pointer/subscript notation with nPtr.

What are the advatages of using the diff ways? Or is it better to stick with one way of doing it ? This is confusing the heck out of me!

2. Ok for the most part its really a matter of preference. And it should be noted that a pointer points to a memory location, an array is a memory location. That said lets go over subscripts:

Code:
```int i;

i = int_array[6];```

Now lets see the same thing with pointer math:
Code:
```int i;

i = *(int_array + 6);```
Disadvantages: Not quite so easy to read or understand

For readable code subscripts are usually prefered since they are a bit easier to follow. However, I do not see any real problems with the point math technique either. Its only harder to understand when you are learning. To me its all the same.

3. ok.But what about the other two ? Maybe this will help.
Code:
```===========================================
//array subscript notation
cout << "Element" << setw(13) << "Value" << endl;
for ( int i = 0; i < 10; i++ )
cout << setw( 7 ) << i << fixed << setprecision( 1 ) << setw(13)
<< numbers[ i ] << endl;
cout << endl << endl;

nPtr = numbers;
nPtr = &numbers[0];
//=====================================================================================
//pointer offset notation using nPtr and offset
for ( int offset = 0; offset < 10; offset++ )
cout << "*(nPtr + " << offset << ") = "
<< *( nPtr + offset ) << endl;

cout << endl;

//=======================================================================================
//use pointer offset notation using array name and offset
for ( int offset1 = 0; offset1 < 10; offset1++ )
cout << "*( numbers + " << offset1 << ") = "
<< *( numbers + offset1 ) << endl;
cout << endl;
//========================================================================================
//using pointer/subscript notation with nPtr
for( int j = 0; j < 10; j++ )
cout << "nPtr[" << j << "] = " << nPtr[j] << endl;

return 0;
}```
Here is four different ways to do the same thing. Why ?
Is one prefered over the other for performance or is it better to code it for readability?

4. >Can anyone tell me why you would have four different ways to access arrays ?
It has to do with the way arrays are treated in an expression. An array is usually converted to a pointer to the first element for just about any use. The array notation for subscripting is syntactic sugar that hides the dereference of an offset from that pointer. Therefore, these two are equivalent:
Code:
```int  a[2];
int *p = a;

a[1]; // Becomes *(a + 1);
p[1]; // Becomes *(p + 1);```
The opposite is also true, the following two are equivalent:
Code:
```*(a + 1); // Same effect as a[1];
*(p + 1); // Same effect as p[1];```
Why four different ways of accessing an array? Because the abstract operations on an array mirror the low level operations of pointers and they can be used interchangeably when it comes to subscripting.

>Is one prefered over the other for performance or is it better to code it for readability?
Use the one you find most readable. They're all basically the same thing and none is noticeably less efficient than any other.

5. Oh sorry about 'the other two' I got lazy on you...Hey Prelude you have a new avatar! nice!

(I haven't been here in a bit)

6. ## Thks peeps

That makes it a little more clear. So i can basicly stick with one style if i like. I was thinking that I would have to incorparate all four different styles depending on what I was doing.ok thks for the quik reply's.This forum is great for quik answers.

Popular pages Recent additions