From 889ab95434fbbf61793b509fe7d93eb6c3ae9e6f Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Thu, 28 Jan 2021 19:17:47 +0100 Subject: [PATCH] Weitere Beispiele 28.1.2021 --- 20210128/hp-20210128.pdf | Bin 170721 -> 170732 bytes 20210128/hp-20210128.tex | 2 +- 20210128/objects-1.c | 28 ++++++++++++++++ 20210128/objects-2.c | 28 ++++++++++++++++ 20210128/objects-3.c | 34 ++++++++++++++++++++ 20210128/objects-3a.c | 34 ++++++++++++++++++++ 20210128/objects-4.c | 34 ++++++++++++++++++++ 20210128/objects-5.c | 34 ++++++++++++++++++++ 20210128/objects-5a.c | 35 ++++++++++++++++++++ 20210128/objects-6.c | 39 ++++++++++++++++++++++ 20210128/objects-7.c | 43 +++++++++++++++++++++++++ 20210128/objects-8.c | 59 +++++++++++++++++++++++++++++++++ 20210128/objects-8a.c | 61 +++++++++++++++++++++++++++++++++++ 20210128/objects-8b.c | 59 +++++++++++++++++++++++++++++++++ 20210128/objects-9.c | 66 +++++++++++++++++++++++++++++++++++++ 20210128/objects-9a.c | 68 +++++++++++++++++++++++++++++++++++++++ 20210128/unions-1.c | 16 +++++++++ 20210128/unions-2.c | 15 +++++++++ 20210128/unions-3.c | 15 +++++++++ 20210128/unions-4.c | 15 +++++++++ 20 files changed, 684 insertions(+), 1 deletion(-) create mode 100644 20210128/objects-1.c create mode 100644 20210128/objects-2.c create mode 100644 20210128/objects-3.c create mode 100644 20210128/objects-3a.c create mode 100644 20210128/objects-4.c create mode 100644 20210128/objects-5.c create mode 100644 20210128/objects-5a.c create mode 100644 20210128/objects-6.c create mode 100644 20210128/objects-7.c create mode 100644 20210128/objects-8.c create mode 100644 20210128/objects-8a.c create mode 100644 20210128/objects-8b.c create mode 100644 20210128/objects-9.c create mode 100644 20210128/objects-9a.c create mode 100644 20210128/unions-1.c create mode 100644 20210128/unions-2.c create mode 100644 20210128/unions-3.c create mode 100644 20210128/unions-4.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T)+F(#C`uShPwWGyJvOW%WvW9u6dtd9`kjjNlf9@ z^_PApuX}UL;s#^d0lN+IKh7tLooQ^=sx^z3xokfDQN8}u!=JfQ^HPdSic%AEL9G0w dEUw~`#G;alqSQ1lLo-V=V?!=gRabvEE&z)QsN4Vm diff --git a/20210128/hp-20210128.tex b/20210128/hp-20210128.tex index 87af238..f291a00 100644 --- a/20210128/hp-20210128.tex +++ b/20210128/hp-20210128.tex @@ -909,7 +909,7 @@ \item Zeiger auf verschiedene Strukturen\\ mit einem gemeinsamen Anteil von Datenfeldern\\ - \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassen} von Objekten + \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassenhierarchie} von Objekten \item Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\ \textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung} diff --git a/20210128/objects-1.c b/20210128/objects-1.c new file mode 100644 index 0000000..1a62866 --- /dev/null +++ b/20210128/objects-1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { &i, &s }; + + return 0; +} diff --git a/20210128/objects-2.c b/20210128/objects-2.c new file mode 100644 index 0000000..a47cfb4 --- /dev/null +++ b/20210128/objects-2.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + return 0; +} diff --git a/20210128/objects-3.c b/20210128/objects-3.c new file mode 100644 index 0000000..ff9224c --- /dev/null +++ b/20210128/objects-3.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", object[i]->content); + + return 0; +} diff --git a/20210128/objects-3a.c b/20210128/objects-3a.c new file mode 100644 index 0000000..a285a87 --- /dev/null +++ b/20210128/objects-3a.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { &i, &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", object[i]->content); + + return 0; +} diff --git a/20210128/objects-4.c b/20210128/objects-4.c new file mode 100644 index 0000000..ef7bffe --- /dev/null +++ b/20210128/objects-4.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", (t_integer *) object[i]->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", (t_string *) object[i]->content); + + return 0; +} diff --git a/20210128/objects-5.c b/20210128/objects-5.c new file mode 100644 index 0000000..820181d --- /dev/null +++ b/20210128/objects-5.c @@ -0,0 +1,34 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", ((t_integer *) object[i])->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", ((t_string *) object[i])->content); + + return 0; +} diff --git a/20210128/objects-5a.c b/20210128/objects-5a.c new file mode 100644 index 0000000..14f1cad --- /dev/null +++ b/20210128/objects-5a.c @@ -0,0 +1,35 @@ +#include <stdio.h> + +typedef struct +{ + int nutzlos; + int type; +} t_base; + +typedef struct +{ + int content; + int type; +} t_integer; + +typedef struct +{ + char *content; + int type; +} t_string; + +int main (void) +{ + t_integer i = { 42, 1 }; + t_string s = { "Hello, world!", 2 }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + if (object[i]->type == 1) + printf ("Integer: %d\n", ((t_integer *) object[i])->content); + else if (object[i]->type == 2) + printf ("String: \"%s\"\n", ((t_string *) object[i])->content); + + return 0; +} diff --git a/20210128/objects-6.c b/20210128/objects-6.c new file mode 100644 index 0000000..86847c9 --- /dev/null +++ b/20210128/objects-6.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == 1) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == 2) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s }; + + for (int i = 0; i < 2; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-7.c b/20210128/objects-7.c new file mode 100644 index 0000000..b9b24ad --- /dev/null +++ b/20210128/objects-7.c @@ -0,0 +1,43 @@ +#include <stdio.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +int main (void) +{ + t_integer i = { T_INTEGER, 42 }; + t_string s = { T_STRING, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8.c b/20210128/objects-8.c new file mode 100644 index 0000000..0c93e8f --- /dev/null +++ b/20210128/objects-8.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_integer *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return p; +} + +t_string *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return p; +} + +int main (void) +{ + t_base *object[] = { (t_base *) new_integer (42), + (t_base *) new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8a.c b/20210128/objects-8a.c new file mode 100644 index 0000000..76c3cc5 --- /dev/null +++ b/20210128/objects-8a.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +char *content = "Ätsch."; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_integer *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return p; +} + +t_string *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return p; +} + +int main (void) +{ + t_base *object[] = { (t_base *) new_integer (42), + (t_base *) new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-8b.c b/20210128/objects-8b.c new file mode 100644 index 0000000..a9d7b5a --- /dev/null +++ b/20210128/objects-8b.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +void print_object (t_base *this) +{ + if (this->type == T_INTEGER) + printf ("Integer: %d\n", ((t_integer *) this)->content); + else if (this->type == T_STRING) + printf ("String: \"%s\"\n", ((t_string *) this)->content); +} + +t_base *new_integer (int i) +{ + t_integer *p = malloc (sizeof (t_integer)); + p->type = T_INTEGER; + p->content = i; + return (t_base *) p; +} + +t_base *new_string (char *s) +{ + t_string *p = malloc (sizeof (t_string)); + p->type = T_STRING; + p->content = s; + return (t_base *) p; +} + +int main (void) +{ + t_base *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-9.c b/20210128/objects-9.c new file mode 100644 index 0000000..41468b0 --- /dev/null +++ b/20210128/objects-9.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_object (t_object *this) +{ + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + return 0; +} diff --git a/20210128/objects-9a.c b/20210128/objects-9a.c new file mode 100644 index 0000000..46431c9 --- /dev/null +++ b/20210128/objects-9a.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +#define T_BASE 0 +#define T_INTEGER 1 +#define T_STRING 2 + +typedef struct +{ + int type; +} t_base; + +typedef struct +{ + int type; + int content; +} t_integer; + +typedef struct +{ + int type; + char *content; +} t_string; + +typedef union +{ + t_base base; + t_integer integer; + t_string string; +} t_object; + +void print_object (t_object *this) +{ + if (this->base.type == T_INTEGER) + printf ("Integer: %d\n", this->integer.content); + else if (this->base.type == T_STRING) + printf ("String: \"%s\"\n", this->string.content); +} + +t_object *new_integer (int i) +{ + t_object *p = malloc (sizeof (t_integer)); + p->integer.type = T_INTEGER; + p->integer.content = i; + return p; +} + +t_object *new_string (char *s) +{ + t_object *p = malloc (sizeof (t_string)); + p->string.type = T_STRING; + p->string.content = s; + return p; +} + +int main (void) +{ + t_object *object[] = { new_integer (42), + new_string ("Hello, world!"), + NULL }; + + for (int i = 0; object[i]; i++) + print_object (object[i]); + + for (int i = 0; object[i]; i++) + free (object[i]); + return 0; +} diff --git a/20210128/unions-1.c b/20210128/unions-1.c new file mode 100644 index 0000000..b75a606 --- /dev/null +++ b/20210128/unions-1.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + int8_t i; + uint8_t u; +} num8_t; + +int main (void) +{ + num8_t test; + test.i = -1; + printf ("%d\n", test.u); + return 0; +} diff --git a/20210128/unions-2.c b/20210128/unions-2.c new file mode 100644 index 0000000..8f3c5d8 --- /dev/null +++ b/20210128/unions-2.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { "Hello" }; + printf ("%lx\n", test.x); + return 0; +} diff --git a/20210128/unions-3.c b/20210128/unions-3.c new file mode 100644 index 0000000..2162c06 --- /dev/null +++ b/20210128/unions-3.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { "Hello" }; + printf ("%ld\n", test.x); + return 0; +} diff --git a/20210128/unions-4.c b/20210128/unions-4.c new file mode 100644 index 0000000..5838bc1 --- /dev/null +++ b/20210128/unions-4.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdint.h> + +typedef union +{ + char s[8]; + uint64_t x; +} num_char_t; + +int main (void) +{ + num_char_t test = { x: 478560413000 }; + printf ("%s\n", test.s); + return 0; +} -- GitLab