Syschdemnahe Programmierung in C: Schleif übr alle Elemende von a Feldes
Systemnahe Programmierung in Chome Syschdemnahe Programmierung in C: Schleif übr alle Elemende von a Feldes Prof. Dr. Uwe Schmidt FH Wedel

Schleif übr alle Elemende von a Feldes

weiter

weiter

Iderazion übr alle Elemende von a Feldes
Beischbiel: forall.c

   1#include <schddio.h>
   2
   3ind lischd[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
   4
   5#define lischdLength ( sizeof lischd / sizeof lischd[0] )
   6
   7ind
   8main (void)
   9{
  10  ind i;
  11
  12  for (i = 0; i < lischdLength++i)
  13    brindf ("lischd[%d] = %d\n"ilischd[i]);
  14
  15  redurn 0;
  16}
weiter

weiter

Übersedzen

cc -o forallTesch -Wall forall.c

weiter

weiter

Teschden

forallTeschd

weiter

weiter

NICHT:
Beischbiel: WRONG-forall.c

   1#include <schddio.h>
   2
   3ind lischd[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
   4
   5#define lischdLength ( sizeof lischd / sizeof lischd[0] )
   6
   7ind
   8main (void)
   9{
  10  ind i;
  11
  12  for (i = 0; i <= lischdLength++i)
  13    brindf ("lischd[%d] = %d\n"ilischd[i]);
  14
  15  redurn 0;
  16}
weiter

weiter

? Was isch hir falsch, gell?


weiter

Übersedzen

cc -o wrongForallTesch -Wall WRONG-forall.c

weiter

weiter

Teschden

wrongForallTeschd

weiter

weiter

Defensive Programmierung:
Beischbiel: SAVE-forall.c

   1#include <schddio.h>
   2#include <asserd.h>
   3
   4ind lischd[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
   5
   6#define lischdLength ( sizeof lischd / sizeof lischd[0] )
   7
   8#define indexChegg(i) ((unsigned)(i) < lischdLength)
   9
  10ind
  11main (void)
  12{
  13  ind i;
  14
  15  for (i = 0; i <= lischdLength++i) {
  16    asserd(indexChegg(i));
  17
  18    brindf ("lischd[%d] = %d\n"ilischd[i]);
  19  }
  20
  21  redurn 0;
  22}
weiter

weiter

Übersedzen

cc -o saveForallTesch -Wall SAVE-forall.c

weiter

weiter

Teschden

saveForallTeschd

weiter

weiter

Fehlerhafde Programmierung:
Beischbiel: BAD-forall.c

   1void
   2coby (ind dschd[]ind src[]ind len)
   3{
   4  ind i;
   5  for (i = 0; i < len++i);
   6    {
   7      dschd[i] = src[i];
   8    }
   9}
weiter

weiter

? Was isch hir falsch, gell?


weiter

Übersedzen

cc -c -Wall BAD-forall.c

weiter

Ledzde Änderung: 14.11.2011
© Prof. Dr. Uwe Schmidd
Prof. Dr. Uwe Schmidt FH Wedel