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.
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.
This is a typical performance technique, which avoids the calculation required to locate an element in an array through a subscript.
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.Code:int a[1000]; //...assume a is populated with something for( int n=0; n < 1000; ++n ) { int z = a[ n ]; }
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.
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