Skip to content
Snippets Groups Projects
Commit 889ab954 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Weitere Beispiele 28.1.2021

parent 749fcab1
No related branches found
No related tags found
No related merge requests found
Showing
with 684 additions and 1 deletion
No preview for this file type
...@@ -909,7 +909,7 @@ ...@@ -909,7 +909,7 @@
\item \item
Zeiger auf verschiedene Strukturen\\ Zeiger auf verschiedene Strukturen\\
mit einem gemeinsamen Anteil von Datenfeldern\\ mit einem gemeinsamen Anteil von Datenfeldern\\
\textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassen} von Objekten \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassenhierarchie} von Objekten
\item \item
Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\ Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\
\textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung} \textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung}
......
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment