Thread: Pointer and Arrays

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    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!
    big146

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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];
    Advantages: easy to read and understand

    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. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    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?
    Last edited by Salem; 06-15-2004 at 12:42 AM. Reason: tagged
    big146

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    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.
    big146

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Pointer Arrays
    By valaris in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 01:20 AM
  2. Pointer and multidimensional arrays
    By disruptivetech in forum C Programming
    Replies: 6
    Last Post: 06-05-2006, 03:04 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer to pointer and 2D arrays
    By rfbu in forum C Programming
    Replies: 6
    Last Post: 06-23-2002, 08:36 PM