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

Beispiele 14.12.2023

parent 9c5f1ed2
No related branches found
No related tags found
No related merge requests found
Showing
with 599 additions and 9 deletions
#include <stdio.h>
char *name[] = { "Anna", "Berthold", "Caesar" };
int main (void)
{
name[3] = "Dieter";
for (int i = 0; i < 4; i++)
printf ("%s\n", name[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *)); /* Speicherplatz für 3 Zeiger anfordern */
name[0] = "Anna";
name[1] = "Berthold";
name[2] = "Caesar";
name[3] = "Dieter"; /* immer noch unzulässig */
for (int i = 0; i < 4; i++)
printf ("%s\n", name[i]);
free (name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *)); /* Speicherplatz für 3 Zeiger anfordern */
name[0] = "Anna";
name[1] = "Berthold";
name[2] = "Caesar";
// name[3] = "Dieter"; /* immer noch unzulässig */
for (int i = 0; i < 3; i++) /* vorher: 4 (unzulässig) */
printf ("%s\n", name[i]);
free (name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *)); /* Speicherplatz für 3 Zeiger anfordern */
name[0] = "Anna";
name[1] = "Berthold";
name[2] = "Caesar";
char **new_name = malloc (4 * sizeof (char *)); /* Speicherplatz für 4 Zeiger anfordern */
for (int i = 0; i < 3; i++)
new_name[i] = name[i];
new_name[3] = "Dieter";
free (name); /* altes Array */
name = new_name; /* alter Zeiger zeigt auf neues Array */
for (int i = 0; i < 4; i++)
printf ("%s\n", name[i]);
free (name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *)); /* Speicherplatz für 3 Zeiger anfordern */
name[0] = "Anna";
name[1] = "Berthold";
name[2] = "Caesar";
char **new_name = malloc (4 * sizeof (char *)); /* Speicherplatz für 4 Zeiger anfordern */
for (int i = 0; i < 3; i++)
new_name[i] = name[i];
new_name[3] = "Dieter";
free (name); /* altes Array */
name = new_name; /* alter Zeiger zeigt auf neues Array */
for (int i = 0; i < 4; i++)
printf ("%s\n", name[i]);
free (name);
free (new_name); /* zeigt auf denselben Speicherplatz wie name --> Fehler */
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *)); /* Speicherplatz für 3 Zeiger anfordern */
name[0] = "Anna";
name[1] = "Berthold";
name[2] = "Caesar";
name = realloc (name, 4 * sizeof (char *)); /* Speicherplatz für 4 Zeiger anfordern */
name[3] = "Dieter";
for (int i = 0; i < 4; i++)
printf ("%s\n", name[i]);
free (name);
return 0;
}
../common/hello-gtk.png
\ No newline at end of file
No preview for this file type
...@@ -65,11 +65,10 @@ ...@@ -65,11 +65,10 @@
\item[6.0] Dynamische Speicherverwaltung \item[6.0] Dynamische Speicherverwaltung
\item[6.1] Konzepte und Ziele \item[6.1] Konzepte und Ziele
\item[6.2] Beispiel: Zahlen und Buchstaben \item[6.2] Beispiel: Zahlen und Buchstaben
\color{black}
\item[6.3] Unions \item[6.3] Unions
\item[6.4] Virtuelle Methoden \item[6.4] Virtuelle Methoden
\vspace*{-\smallskipamount} \item[6.5] Beispiel: Graphische Benutzeroberfläche (GUI)
\item[\dots] \item[6.6] Ausblick: C++
\end{itemize} \end{itemize}
\item[\textbf{7}] \textbf{Datenstrukturen} \item[\textbf{7}] \textbf{Datenstrukturen}
\end{itemize} \end{itemize}
...@@ -337,8 +336,8 @@ ...@@ -337,8 +336,8 @@
\put(-2.75,-0.05){\tikz{\draw[thick](0,0.25)--(2.5,-0.05);% \put(-2.75,-0.05){\tikz{\draw[thick](0,0.25)--(2.5,-0.05);%
\draw[thick](-0.1,-0.05)--(2.5,0.3);}} \draw[thick](-0.1,-0.05)--(2.5,0.3);}}
\put(1.5,-1.1){\begin{rotate}{7}\large\bf\textarrow\ \put(1.5,-1.1){\begin{rotate}{7}\large\bf\textarrow\
% kommt gleich kommt gleich
nächste Woche % nächste Woche
\end{rotate}} \end{rotate}}
\end{picture} \end{picture}
Zeiger, die im Objekt gespeichert sind\\ Zeiger, die im Objekt gespeichert sind\\
...@@ -478,8 +477,6 @@ ...@@ -478,8 +477,6 @@
\end{frame} \end{frame}
\iffalse
\subsection{Unions} \subsection{Unions}
\begin{frame}[fragile] \begin{frame}[fragile]
...@@ -830,6 +827,4 @@ ...@@ -830,6 +827,4 @@
\end{frame} \end{frame}
\fi
\end{document} \end{document}
#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[] = { (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 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;
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 <stdlib.h>
typedef struct
{
} t_base;
typedef struct
{
int content;
} t_integer;
typedef struct
{
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.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (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 <stdlib.h>
#include <string.h>
typedef struct
{
} t_base;
typedef struct
{
int content;
} t_integer;
typedef struct
{
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
printf ("Wenn dies eine Integer ist, dann lautet sie: %d.\n", this->integer.content);
printf ("Wenn dies ein String ist, ist er %zu Zeichen lang.\n",
strlen (this->string.content));
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment