Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen gezeigt.

Link zu dieser Vergleichsansicht

tux:typecasting [2011/11/30 14:51]
wikisysop
tux:typecasting [2011/11/30 20:02] (aktuell)
wikisysop [Explizite Typumwandlung]
Zeile 30: Zeile 30:
 Bei der >><​fc #​008000>​expliziten</​fc><<​ Typumwandlung wird die Typumwandlung im Code vorgenommen. Es gilt dabei folgende Syntax: >><​fc #​008000>​(Zieltyp)Ausdruck</​fc><<,​ wobei >><​fc #​008000>​Zieltyp</​fc><<​ der Datentyp ist, zu dem >><​fc #​008000>​Ausdruck</​fc><<​ konvertiert werden soll. Bei der >><​fc #​008000>​expliziten</​fc><<​ Typumwandlung wird die Typumwandlung im Code vorgenommen. Es gilt dabei folgende Syntax: >><​fc #​008000>​(Zieltyp)Ausdruck</​fc><<,​ wobei >><​fc #​008000>​Zieltyp</​fc><<​ der Datentyp ist, zu dem >><​fc #​008000>​Ausdruck</​fc><<​ konvertiert werden soll.
  
-<code c|Typecasting integer zu float zu double>#​include <​stdio.h>​+<code c|pointer_types_1>#​include <​stdio.h>​
  
-int i = 3;       // Die Variable i wird als Integer definiert und mit dem Wert 3 belegt. +int main() { 
-float f;         // Die Variable f wird als Float definiert. + int i;
-double d;        // Die Variable d wird als Double definiert. +
-double x = 0.1 // Die Variable x wird als Double definiert und mit dem Wert 0,1 belegt.+
  
-main (void) ​{+ char char_array[5] = {'​a',​ '​b',​ '​c',​ '​d',​ '​e'​};​ 
 + int int_array[5] = {1, 2, 3, 4, 5};
  
-  printf("​Typecasting von auf float: %f\n", (float)i)                          // Hier wird die Integervariable für die printf-Ausgabe per explizites Typcasting in ein Float umgewandelt. + char *char_pointer;​ 
-  printf("​Typecasting von i auf double: ​%lf\n", ​(double)i);                        // Hier wird die Integervariable für die printf-Ausgabe per explizites Typcasting in ein Double umgewandelt. + int *int_pointer;​ 
-  ​printf("​0.als double definiert = %.30lf\n",​ x)                               // Hier wird die Double Variable x (-> 0,1ohne Typecasting aber mit 30 Nachkommastellen ausgegeben. + 
-  printf("​0.1 als double definiert und als float ausgegeben = %.30lf\n",(float)x); // Hier wird die Double Variable x (0,1) per Typecasting für die Ausgabe in ein Float umgewandelt und mit 30 Nachkommastellen ausgegeben.+ char_pointer = char_array;​ 
 + int_pointer = int_array;​ 
 + 
 + for(i=0; < 5i++) {  
 + printf("​[integer pointer] points to %p, which contains the integer %d\n", ​int_pointer,​ *int_pointer); 
 + int_pointer = int_pointer + 1; 
 +
 +  
 + for(i=0; i < 5; i++ 
 + printf("​[char pointer] points to %p, which contains the char '%c'\n", ​char_pointer,​ *char_pointer); 
 + char_pointer = char_pointer + 1
 + }
 }</​code>​ }</​code>​
  
-<​xterm>​$ <fc #​008000>​./​cast_8</​fc>​ +<​xterm>​$ <fc #​008000>​./​pointer_types_1</fc>  
-Typecasting von i auf float: 3.000000 +[integer pointer] points to 0xbfa86134, which contains the integer 1 
-Typecasting von i auf double: ​3.000000 +[integer pointer] points to 0xbfa86138, which contains the integer 2 
-0.1 als double definiert = 0.100000000000000005551115123126 +[integer pointer] points to 0xbfa8613c, which contains the integer ​
-0.1 als double definiert und als float ausgegeben = 0.100000001490116119384765625000</​xterm>​+[integer pointer] points to 0xbfa86140, which contains the integer 4 
 +[integer pointer] points to 0xbfa86144, which contains the integer 5 
 +[char pointer] points to 0xbfa86157, which contains the char '​a'​ 
 +[char pointer] points to 0xbfa86158, which contains the char '​b'​ 
 +[char pointer] points to 0xbfa86159, which contains the char '​c'​ 
 +[char pointer] points to 0xbfa8615a, which contains the char '​d'​ 
 +[char pointer] points to 0xbfa8615b, which contains the char '​e'​</​xterm>​
tux/typecasting.1322661114.txt.gz (3808 views) · Zuletzt geändert: 2011/11/30 14:51 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