Correction 2: priorité des opérateurs

Publié le par JJ

Voilà il y avait biensur 2 erreurs:
    int tab[10];

    /*....*/

    for( i=10 ; i>=0 ; i--){
        tab[i]=i;
    }

ici un dépassement de tableau pour i = 10;  On affecte 10 cases indices 0 à 9 dans le tableau et on commence par appeler la 11e ;)

L'autre est évidente lorsqu'on fait la trace:

~/Desktop$ ./out
Bonjour!
 1e partie
lu tableau: 10 à l'indice 10
lu tableau: 9 à l'indice 9
lu tableau: 8 à l'indice 8
lu tableau: 7 à l'indice 7
lu tableau: 6 à l'indice 6
lu tableau: 5 à l'indice 5
lu tableau: 4 à l'indice 4
lu tableau: 3 à l'indice 3
lu tableau: 2 à l'indice 2
lu tableau: 1 à l'indice 1
lu tableau: 0 à l'indice 0
2e partie
lu tableau: 0 à l'indice 1
lu tableau: 1 à l'indice 2
lu tableau: 2 à l'indice 3
lu tableau: 3 à l'indice 4
lu tableau: 4 à l'indice 5
lu tableau: 5 à l'indice 6
lu tableau: 6 à l'indice 7
lu tableau: 7 à l'indice 8
lu tableau: 8 à l'indice 9
lu tableau: 9 à l'indice 10


voilà...  pourquoi?
la réponce est ici:
        j = tab[i++];
        printf("lu tableau: %d à l'indice %d\n", j, i);

l'affectation se fait AVANT la somme. piege.

(vous constaterez que les commentaires // sont à banir avec la nouvelle version 3.4.5 de gcc)

Publié dans Corrections

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article