Thread: Can anyone show me examples of these two questions.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    32

    Can anyone show me examples of these two questions.

    One approach is to subscript the array in a loop over the elements of the array.




    The other approach is to use a combination of advancing the pointer ptr thru the array and dereferencing it.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    147
    This is a typical performance technique, which avoids the calculation required to locate an element in an array through a subscript.

    Code:
    int a[1000];
    
    //...assume a is populated with something
    
    for( int n=0; n < 1000; ++n )
        {
          int z = a[ n ];
        }
    In this case, n is referencing an element in the array, and in theory (that is, not accounting potential compiler optimization by recognizing this simple loop), there is a calculation of n * sizeof( int ) required for each reference in the array.

    This is the same result, but the reference won't calculate n * sizeof( int ) for each access in the array;

    Code:
    int * ptr = a;
    int * lim = ptr + 1000;
    
    while( ptr < lim )
         {
          int z = *ptr;
    
          ++ptr;
         }

    In the case the array refers to a struct, then the form ptr->m is often used.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sjcc View Post
    The other approach is to use a combination of advancing the pointer ptr thru the array and dereferencing it.
    You can use this approach to pass an entire array to a function. Consider this example.
    Code:
    void disp(int *,int);
    int main(void)
    {
    int a[]={1,2,3,4,5};
    disp(a,5);//passing the base adress of the array
    return 0;
    }
    void disp(int *b,int n)
    {
    int i;
    for(i=0;i<=n-1;i++)
    printf("%d",*j++);
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 03:57 PM
  3. Some C test questions: challenge
    By Mister C in forum C Programming
    Replies: 47
    Last Post: 09-10-2002, 06:47 AM
  4. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM