9
Itération dans un tableau
- #include <stdlib.h>
- #include <stdio.h>
- #define TABSIZE 4
- main() {
- float tab[ TABSIZE ];
- float *pf;
- int i;
- printf("Enter 4 reals: \n");
- i = 0;
- while ( i < TABSIZE ) {
- printf("%d: ", i+1);
- switch( scanf("%f", &tab[i])) {
- case 0:
- exit(1);
- case 1:
- ++i;
- break;
- default:
- scanf("%*s"); /* swallow */
- break;
- }
- }
- printf("You have entered (in reverse order): \n");
- /* iteration with an index */
- for ( i = TABSIZE-1; i >= 0; i-- )
- printf("%d: %f\n", i+1, tab[ i ]);
- printf("Here are their addresses in memory: \n");
- /* iteration with a pointer */
- for ( pf = tab; pf < tab+TABSIZE; pf++ )
- printf("%f\t-> 0x%x\n", *pf, (unsigned)pf);
- exit(0);
- }
Commentaires