|
| 1 | +#include <stdio.h> |
| 2 | +void fun(int *bPtr); |
| 3 | + int main( void ) |
| 4 | + { |
| 5 | + int b[] = { 10, 20, 30, 40 }; /* initialize array b */ |
| 6 | + int *bPtr = b; /* set bPtr to point to array b */ |
| 7 | + int i,offset; |
| 8 | + fun(bPtr); |
| 9 | +printf("%u \n",b); |
| 10 | + /* output array b using array subscript notation */ |
| 11 | + printf( "Array b printed with:\nArray subscript notation\n" ); |
| 12 | + |
| 13 | + /* loop through array b */ |
| 14 | + for ( i = 0; i < 4; i++ ) { |
| 15 | + printf( "b[ %d ] = %d\n", i,b[ i ] ); |
| 16 | + } /* end for */ |
| 17 | + |
| 18 | + /* output array b using array name and pointer/offset notation */ |
| 19 | + printf( "\nPointer/offset notation where\n" |
| 20 | + "the pointer is the array name\n" ); |
| 21 | + /* loop through array b */ |
| 22 | + for ( offset = 0; offset < 4; offset++ ) { |
| 23 | + printf( "*( %u + %d ) = %d\n",bPtr, offset, *(b + offset) ); |
| 24 | + } /* end for */ |
| 25 | + |
| 26 | +return 0; |
| 27 | +} |
| 28 | + void fun(int *bPtr){ |
| 29 | + |
| 30 | + int i; /* counter */ |
| 31 | + int offset; /* counter */ |
| 32 | +printf("%u \n",bPtr); |
| 33 | + |
| 34 | +/* output array b using bPtr and array subscript notation */ |
| 35 | + printf( "\nPointer subscript notation\n" ); |
| 36 | + |
| 37 | + /* loop through array b */ |
| 38 | + for ( i = 0; i < 4; i++ ) { |
| 39 | + printf( "bPtr[ %d ] = %d\n", i,bPtr[ i ] ); |
| 40 | + } /* end for */ |
| 41 | + |
| 42 | + /* output array b using bPtr and pointer/offset notation */ |
| 43 | + printf( "\nPointer/offset notation\n" ); |
| 44 | + |
| 45 | + /* loop through array b */ |
| 46 | + for ( offset = 0; offset < 4; offset++ ) { |
| 47 | + printf( "*( bPtr + %d ) = %d\n", offset, *( bPtr + offset) ); |
| 48 | + } /* end for */ |
| 49 | + |
| 50 | + } |
0 commit comments