Systemnahe Programmierung in C: Schleife über alle Elemente eines Feldes |
1#include <stdio.h>
2
3int list[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
4
5#define listLength ( sizeof list / sizeof list[0] )
6
7int
8main (void)
9{
10 int i;
11
12 for (i = 0; i < listLength; ++i)
13 printf ("list[%d] = %d\n", i, list[i]);
14
15 return 0;
16}
|
1#include <stdio.h>
2
3int list[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
4
5#define listLength ( sizeof list / sizeof list[0] )
6
7int
8main (void)
9{
10 int i;
11
12 for (i = 0; i <= listLength; ++i)
13 printf ("list[%d] = %d\n", i, list[i]);
14
15 return 0;
16}
|
1#include <stdio.h>
2#include <assert.h>
3
4int list[] = { 13, 56, 23, 1, 89, 58, 20, 125, 86, 3 };
5
6#define listLength ( sizeof list / sizeof list[0] )
7
8#define indexCheck(i) ((unsigned)(i) < listLength)
9
10int
11main (void)
12{
13 int i;
14
15 for (i = 0; i <= listLength; ++i) {
16 assert(indexCheck(i));
17
18 printf ("list[%d] = %d\n", i, list[i]);
19 }
20
21 return 0;
22}
|
1void
2copy (int dst[], int src[], int len)
3{
4 int i;
5 for (i = 0; i < len; ++i);
6 {
7 dst[i] = src[i];
8 }
9}
|
Letzte Änderung: 14.11.2011 | © Prof. Dr. Uwe Schmidt |