Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
tux:typecasting [2011/11/21 17:02] wikisysop [C -> Typecasting] |
tux:typecasting [2011/11/30 20:02] (aktuell) wikisysop [Explizite Typumwandlung] |
||
---|---|---|---|
Zeile 29: | Zeile 29: | ||
====Explizite Typumwandlung==== | ====Explizite Typumwandlung==== | ||
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|pointer_types_1>#include <stdio.h> | ||
+ | |||
+ | int main() { | ||
+ | int i; | ||
+ | |||
+ | char char_array[5] = {'a', 'b', 'c', 'd', 'e'}; | ||
+ | int int_array[5] = {1, 2, 3, 4, 5}; | ||
+ | |||
+ | char *char_pointer; | ||
+ | int *int_pointer; | ||
+ | |||
+ | char_pointer = char_array; | ||
+ | int_pointer = int_array; | ||
+ | |||
+ | for(i=0; i < 5; i++) { | ||
+ | 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> | ||
+ | |||
+ | <xterm>$ <fc #008000>./pointer_types_1</fc> | ||
+ | [integer pointer] points to 0xbfa86134, which contains the integer 1 | ||
+ | [integer pointer] points to 0xbfa86138, which contains the integer 2 | ||
+ | [integer pointer] points to 0xbfa8613c, which contains the integer 3 | ||
+ | [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> |