Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
tux:arrays [2011/12/04 18:19] wikisysop [Eindimensionale Arrays] |
tux:arrays [2011/12/11 20:45] (aktuell) wikisysop |
||
---|---|---|---|
Zeile 34: | Zeile 34: | ||
}</code> | }</code> | ||
- | <xterm>$ ./sizeof_array | + | <xterm>$ <fc #008000>./sizeof_array</fc> |
Das Array alpha belegt im Speicher 16 Byte Platz | Das Array alpha belegt im Speicher 16 Byte Platz | ||
Der Datentyp int (Integer) belegt pro Element 4 Byte Platz im Speicher | Der Datentyp int (Integer) belegt pro Element 4 Byte Platz im Speicher | ||
Zeile 52: | Zeile 52: | ||
}</code> | }</code> | ||
- | <xterm>$ ./char_arrays_1 | + | <xterm>$ <fc #008000>./char_arrays_1</fc> |
Anzahl der Elemente des Strings 'Hello, world!' ist 14 | Anzahl der Elemente des Strings 'Hello, world!' ist 14 | ||
Die Groesse eines Element des Datentyps char ist 1 Byte</xterm> | Die Groesse eines Element des Datentyps char ist 1 Byte</xterm> | ||
- | Ein char-Array kann auf mehrere Weisen initialisiert werden: | + | Ein char-Array kann auf mehrere Weisen initialisiert und auch ausgegeben werden: |
+ | <code c|int_char_arrays.c>#include <stdio.h> | ||
+ | |||
+ | char alpha01[14] = {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0}; | ||
+ | char alpha02[14] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'}; | ||
+ | char alpha03[] = "Hello, world!"; | ||
+ | int i; | ||
+ | |||
+ | int main(void) { | ||
+ | printf("%s\n", alpha01); | ||
+ | printf("%s\n", alpha02); | ||
+ | printf("%s\n", alpha03); | ||
+ | |||
+ | for(i=0; i<sizeof(alpha01); i++) { | ||
+ | printf("%c", alpha01[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | |||
+ | for(i=0; i<sizeof(alpha02); i++) { | ||
+ | printf("%c", alpha02[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | |||
+ | for(i=0; i<sizeof(alpha03); i++) { | ||
+ | printf("%c", alpha03[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | |||
+ | return 0; | ||
+ | }</code> | ||
+ | |||
+ | Dieser Coder gibt erst mal immer den gleichen String >><fc #008000>Hello, world!</fc><< aus, welcher auf unterschiedliche Weise in ein char-Array kopiert wurde. | ||
+ | |||
+ | <xterm>$ <fc #008000>./int_char_arrays</fc> | ||
+ | Hello, world! | ||
+ | Hello, world! | ||
+ | Hello, world! | ||
+ | Hello, world! | ||
+ | Hello, world! | ||
+ | Hello, world! | ||
+ | </xterm> | ||
+ | |||
+ | Damit man den Inhalt der Arrays >>alpha01<< und >>alpha02<< nicht selber ausrechnen und tippen muss, kann man die Ausgabe mit geeigneten Formatierungszeichen so hinbiegen, dass man sie so schon übernehmen kann: | ||
+ | |||
+ | <code c|out_char_arrays.c>#include <stdio.h> | ||
+ | |||
+ | char alpha[] = "Hello, world!"; | ||
+ | int i; | ||
+ | |||
+ | int main(void) { | ||
+ | |||
+ | for(i=0; i<sizeof(alpha); i++) { | ||
+ | printf("%d, ", alpha[i]); /* %d gibt den Inhalt als dezimale Ganzzahl aus */ | ||
+ | } | ||
+ | printf("\n"); | ||
+ | |||
+ | for(i = 0; i < sizeof(alpha) -1; i++) { | ||
+ | printf("'%c', ", alpha[i]); /* %c gibt den Character, also das Zeichen als solches aus */ | ||
+ | } | ||
+ | |||
+ | printf("\n"); | ||
+ | return 0; | ||
+ | }</code> | ||
+ | |||
+ | <xterm>$ <fc #008000>./out_char_arrays</fc> | ||
+ | 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0, | ||
+ | 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!',</xterm> | ||