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

1. ## 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. 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. Originally Posted by sjcc
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++);
}```