Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu dieser Vergleichsansicht

tux:arrays [2011/12/04 18:05]
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 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>​
 +
tux/arrays.1323018321.txt.gz (8034 views) · Zuletzt geändert: 2011/12/04 18:05 von wikisysop
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0