Pointer and Arrays
Can anyone tell me why you would have four different ways to access arrays ?
 Array subscript notation.
 pointer offset notation using nPtr and offset.
 pointer offset notation using array name and offset
 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!
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:
Advantages: easy to read and understand
i = int_array;
Now lets see the same thing with pointer math:
Disadvantages: Not quite so easy to read or understand
i = *(int_array + 6);
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.
ok.But what about the other two ? Maybe this will help.
Here is four different ways to do the same thing. Why ?
//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;
//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;
Is one prefered over the other for performance or is it better to code it for readability?
>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:
The opposite is also true, the following two are equivalent:
int *p = a;
a; // Becomes *(a + 1);
p; // Becomes *(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.
*(a + 1); // Same effect as a;
*(p + 1); // Same effect as p;
>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.
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)
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.