Select Git revision
hp-20191031.tex
Forked from
Peter Gerwinski / hp
281 commits behind the upstream repository.
Peter Gerwinski authored
hp-20191031.tex 18.14 KiB
% hp-20191031.pdf - Lecture Slides on Low-Level Programming
% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Einführung in C: Arrays und Strings und Zeichen, Strukturen, Dateien und Fehlerbehandlung
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{pdftricks}
\usepackage{tikz}
\begin{psinputs}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\renewcommand*\familydefault{\sfdefault}
\usepackage{pstricks,pst-grad}
\end{psinputs}
\title{Hardwarenahe Programmierung}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{31.\ Oktober 2019}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}}
\item[\textbf{2}] \textbf{Einführung in C}
\begin{itemize}
\vspace*{-\smallskipamount}
\item[\dots]
\item[2.5] Verzweigungen
\item[2.6] Schleifen
\item[2.7] Strukturierte Programmierung
\item[2.8] Seiteneffekte
\item[2.9] Funktionen
\color{medgreen}
\item[2.10] Zeiger
\color{orange}
\item[2.11] Arrays und Strings
\color{red}
\item[2.12] Strukturen
\item[2.13] Dateien und Fehlerbehandlung
\color{black}
\item[2.14] Parameter des Hauptprogramms
\item[2.15] String-Operationen
\end{itemize}
\color{gray}
\item[\textbf{3}] \textbf{Bibliotheken}
\vspace*{-\smallskipamount}
\item[\textbf{\dots}]
% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung}
% \item[\textbf{5}] \textbf{Algorithmen}
% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke}
\end{itemize}
\end{frame}
\setcounter{section}{1}
\section{Einführung in C}
\setcounter{subsection}{9}
\subsection{Zeiger}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
#include <stdio.h>
void calc_answer (int *a)
{
*a = 42;
}
int main (void)
{
int answer;
calc_answer (&answer);
printf ("The answer is %d.\n", answer);
return 0;
}
\end{lstlisting}
% \pause
\vspace{-5cm}\hspace{5cm}%
\begin{minipage}{7cm}
\begin{itemize}
\item
\lstinline{*a} ist eine \lstinline{int}.
% \pause
\item
unärer Operator \lstinline{*}:\\
Pointer-Dererefenzierung
% \pause
\arrowitem
\lstinline{a} ist ein Zeiger (Pointer) auf eine \lstinline{int}.
% \pause
\bigskip
\item
unärer Operator \lstinline{&}: Adresse
\end{itemize}
\end{minipage}
\end{frame}
\subsection{Arrays und Strings}
\begin{frame}[fragile]
\showsubsection
Ein Zeiger zeigt auf eine Variable\only<2->{ und deren Nachbarn}.
\bigskip
\pause
\pause
\begin{onlyenv}<1-8>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[5] = { 2, 3, 5, 7, 11 };
int *p = prime;
for (int i = 0; i < 5; i++)
printf ("%d\n", *(p + i));
return 0;
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<9>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[5] = { 2, 3, 5, 7, 11 };
int *p = prime;
for (int i = 0; i < 5; i++)
printf ("%d\n", p[i]);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<10>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int i = 0; i < 5; i++)
printf ("%d\n", prime[i]);
return 0;
}
¡ ¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<11>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime;
p < prime + 5; p++)
printf ("%d\n", *p);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<12>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[6] = { 2, 3, 5, 7, 11, 0 };
for (int *p = prime; *p; p++)
printf ("%d\n", *p);
return 0;
}
¡ ¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<13->
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
int prime[] = { 2, 3, 5, 7, 11, 0 };
for (int *p = prime; *p; p++)
printf ("%d\n", *p);
return 0;
}
¡ ¿
\end{lstlisting}
\end{onlyenv}
\pause
\vspace{-3.05cm}\hspace{5.5cm}%
\begin{minipage}{6.5cm}
\begin{itemize}
\item
\lstinline{prime} ist \alt<5->{ein Array}{eine Ansammlung} von\\fünf ganzen Zahlen.
\pause
\pause
\item
\only<6-9>{\begin{picture}(0,0)
\color{red}
\put(-1.6,0.1){\tikz{\draw[-latex](0.0,0.0)--(-1,0);}}
\end{picture}}%
\lstinline{prime} ist ein Zeiger auf eine \lstinline{int}.
\pause
\item
\lstinline{p + i} ist ein Zeiger\\
auf den \lstinline{i}-ten Nachbarn von \lstinline{*p}.
\pause
\item
\lstinline{*(p + i)} ist der \lstinline{i}-te Nachbar von \lstinline{*p}.
\pause
\item
Andere Schreibweise:\\
\lstinline{p[i]} statt \lstinline{*(p + i)}
\pause
\pause
\item
Zeiger-Arithmetik:\\
\lstinline{p++} rückt den Zeiger \lstinline{p}\\
um eine \lstinline{int} weiter.
\pause
\pause
\item
Array ohne \only<14->{explizite }Längenangabe:\\
Compiler zählt selbst
\vspace*{-1cm}
\pause
\begin{picture}(0,0)
\put(-5.2,1.0){\makebox(0,0)[br]{\color{red}\bf\shortstack{Die Länge des Arrays\\ist \emph{nicht\/} veränderlich!}}}
\end{picture}
\end{itemize}
\end{minipage}
\end{frame}
% \begin{frame}[fragile]
% \showsubsection
%
% \begin{lstlisting}
% #include <stdio.h>
%
% int main (void)
% {
% char hello_world[] = "Hello, world!\n";
% int i = 0;
% while (hello_world[i] != 0)
% printf ("%d", hello_world[i++]);
% return 0;
% }
% \end{lstlisting}
% \end{frame}
% \begin{frame}[fragile]
% \showsubsection
%
% \begin{lstlisting}
% #include <stdio.h>
%
% int main (void)
% {
% char hello_world[] = "Hello, world!\n";
% int i = 0;
% while (hello_world[i])
% printf ("%d", hello_world[i++]);
% return 0;
% }
% \end{lstlisting}
% \end{frame}
% \begin{frame}[fragile]
% \showsubsection
%
% \begin{lstlisting}
% #include <stdio.h>
%
% int main (void)
% {
% char hello_world[] = "Hello, world!\n";
% char *p = hello_world;
% while (*p)
% printf ("%c", *p++);
% return 0;
% }
% \end{lstlisting}
% \end{frame}
\begin{frame}[fragile]
\showsubsection
\begin{onlyenv}<1-6>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
char hello[] = "Hello, world!\n";
for (char *p = hello; *p; p++)
printf ("%d", *p);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<7>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
char hello[] = "Hello, world!\n";
for (char *p = hello; *p; p++)
printf ("%c", *p);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<8>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
char hello[] = "Hello, world!\n";
printf ("%s", hello);
return 0;
}
¡ ¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<9>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
char *hello = "Hello, world!\n";
printf ("%s", hello);
return 0;
}
¡ ¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<10>
\begin{lstlisting}[gobble=6]
#include <stdio.h>
int main (void)
{
char *hello = "Hello, world!\n";
while (*hello)
printf ("%c", *hello++);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\vspace{-1.7cm}\hfill
\begin{minipage}{6.8cm}
\begin{itemize}
\pause[2]
\item
Ein \lstinline{char} ist eine kleinere \lstinline{int}.
\pause
\item
Ein "`String"' in C ist ein Array von \lstinline{char}s\only<4->{,\\
also ein Zeiger auf \lstinline{char}s}\only<5->{\\
also ein Zeiger auf (kleinere) Integer}.
\pause
\pause
\pause
\item
Der letzte \lstinline{char} muß 0 sein.\\
Er kennzeichnet das Ende des Strings.
\pause
\item
Die Formatspezifikation\\
entscheidet über die Ausgabe:\\[\smallskipamount]
\begin{tabular}{ll}
\lstinline|%d|\hspace*{0.5em}dezimal
& \lstinline|%c|\hspace*{0.5em}Zeichen\\
\lstinline|%x|\hspace*{0.5em}hexadezimal
\pause
& \lstinline|%s|\hspace*{0.5em}String
\end{tabular}
\vspace*{-1cm}
\end{itemize}
\end{minipage}
\end{frame}
\addtocounter{subsection}{-1}
\subsection{Arrays und Strings \protect\color{gray}und Zeichen}
\begin{frame}[fragile]
\showsubsection
\emph{"`Alles ist Zahl."'\/} -- Schule der Pythagoreer, 6.\ Jh.\ v.\,Chr.
\medskip
\begin{center}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{r}
\lstinline|"Hello"|\\
\lstinline|'H'|\\
\lstinline|'a' + 4|
\end{tabular}
\renewcommand{\arraystretch}{1.0}
\begin{tabular}{c}
ist nur eine andere\\
Schreibweise für
\end{tabular}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{l}
\lstinline|{ 72, 101, 108, 108, 111, 0 }|\\
\lstinline|72|\\
\lstinline|'e'|
\end{tabular}
\renewcommand{\arraystretch}{1.0}
\end{center}
\begin{itemize}
\item
Welchen Zahlenwert hat \lstinline{'*'} im Zeichensatz (normalerweise: ASCII)?\\
Welches Zeichen entspricht dem Zahlenwert \lstinline{71}?
\smallskip
\begin{lstlisting}[gobble=8]
printf ("%d\n", '*');
printf ("%c\n", 71);
\end{lstlisting}
\medskip
\item
Ist \lstinline{char ch} ein Großbuchstabe?
\smallskip
\begin{lstlisting}[gobble=8]
if (ch >= 'A' && ch <= 'Z')
...
\end{lstlisting}
\smallskip
\item
Groß- in Kleinbuchstaben umwandeln
\smallskip
\begin{lstlisting}[gobble=8]
ch += 'a' - 'A';
\end{lstlisting}
\end{itemize}
\vspace*{-1cm}
\end{frame}
\subsection{Strukturen}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
int main (void)
{
date today = { 24, 10, 2019 };
printf ("%d.%d.%d\n", today.day, today.month, today.year);
return 0;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\showsubsection
\vspace*{0.9mm}
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
void set_date (date *d)
{
(*d).day = 24;
(*d).month = 10;
(*d).year = 2019;
}¿
\end{lstlisting}
\end{minipage}%
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡int main (void)
{
date today;
set_date (&today);
printf ("%d.%d.%d\n", today.day,
today.month, today.year);
return 0;
}¿
\end{lstlisting}
\end{minipage}
\end{frame}
\begin{frame}[fragile]
\showsubsection
\vspace*{0.9mm}
\begin{minipage}[b]{6cm}
\begin{lstlisting}[gobble=6]
¡#include <stdio.h>
typedef struct
{
char day, month;
int year;
}
date;
void set_date (date *d)
{
d->day = 24;
d->month = 10;
d->year = 2019;
}¿
\end{lstlisting}
\end{minipage}%
\begin{minipage}[b]{6cm}
\hspace*{-1cm}%
\lstinline{foo->bar}
ist Abkürzung für
\lstinline{(*foo).bar}
\bigskip
\visible<2->{%
\hspace*{-1cm}%
Eine Funktion, die mit einem \lstinline{struct} arbeitet,\\
\hspace*{-1cm}%
kann man eine \newterm{Methode\/} des \lstinline{struct} nennen.}
\bigskip
\bigskip
\begin{lstlisting}[gobble=6]
¡int main (void)
{
date today;
set_date (&today);
printf ("%d.%d.%d\n", today.day,
today.month, today.year);
return 0;
}¿
\end{lstlisting}
\end{minipage}
\end{frame}
\subsection{Dateien und Fehlerbehandlung}
\begin{frame}[fragile]
\showsubsection
\vspace*{-0.2925cm}
\begin{minipage}[t]{6cm}
\begin{onlyenv}<1>
\begin{lstlisting}[gobble=8]
¡#include <stdio.h>
int main (void)
{
FILE *f = fopen ("fhello.txt", "w");
fprintf (f, "Hello, world!\n");
fclose (f);
return 0;
}¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[gobble=8]
¡#include <stdio.h>
int main (void)
{
FILE *f = fopen ("fhello.txt", "w");
if (f)
{
fprintf (f, "Hello, world!\n");
fclose (f);
}
return 0;
}¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{lstlisting}[gobble=8]
¡#include <stdio.h>
#include <errno.h>
int main (void)
{
FILE *f = fopen ("fhello.txt", "w");
if (f)
{
fprintf (f, "Hello, world!\n");
fclose (f);
}
else
fprintf (stderr, "error #%d\n", errno);
return 0;
}¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{lstlisting}[gobble=8]
¡#include <stdio.h>
#include <errno.h>
#include <string.h>
int main (void)
{
FILE *f = fopen ("fhello.txt", "w");
if (f)
{
fprintf (f, "Hello, world!\n");
fclose (f);
}
else
{
char *msg = strerror (errno);
fprintf (stderr, "%s\n", msg);
}
return 0;
}¿
\end{lstlisting}
\vspace*{-1cm}
\end{onlyenv}
\begin{onlyenv}<5->
\begin{lstlisting}[gobble=8]
¡#include <stdio.h>
#include <errno.h>
#include <er¡ror.h>
int main (void)
{
FILE *f = fopen ("fhello.txt", "w");
if (!f)
error (1, errno, "cannot open file");
fprintf (f, "Hello, world!\n");
fclose (f);
return 0;
}
\end{lstlisting}
\end{onlyenv}
\end{minipage}\pause\hspace*{-1.5cm}%
\begin{minipage}[t]{8.5cm}
\bigskip
\only<3->{\bigskip}
\begin{itemize}
\item
Wenn die Datei nicht geöffnet werden kann,\\
gibt \lstinline{fopen()} den Wert \lstinline{NULL} zurück.
\pause
\medskip
\item
\addtolength{\leftskip}{1cm}
Die globale Variable \lstinline{int errno}\\
enthält dann die Nummer des Fehlers.\\
Benötigt: \lstinline{#include <errno.h>}
\pause
\medskip
\only<5->{\bigskip}
\item
Die Funktion \lstinline{strerror()} wandelt \lstinline{errno}\\
in einen Fehlermeldungstext um.\\
Benötigt: \lstinline{#include <string.h>}
\pause
\medskip
\item
\addtolength{\leftskip}{-1.5cm}
Die Funktion \lstinline{error()} gibt eine Fehlermeldung aus\\
und beendet das Programm.\\
Benötigt: \lstinline{#include <er¡¿ror.h>}
\pause
\medskip
\item
\textbf{Niemals Fehler einfach ignorieren!}
\end{itemize}
\addtolength{\leftskip}{0.5cm}
\end{minipage}
\end{frame}
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}}
\item[\textbf{2}] \textbf{Einführung in C}
\begin{itemize}
\vspace*{-\smallskipamount}
\item[\dots]
\item[2.5] Verzweigungen
\item[2.6] Schleifen
\item[2.7] Strukturierte Programmierung
\item[2.8] Seiteneffekte
\item[2.9] Funktionen
\item[2.10] Zeiger
\color{medgreen}
\item[2.11] Arrays und Strings
\item[2.12] Strukturen
\item[2.13] Dateien und Fehlerbehandlung
\color{red}
\item[2.14] Parameter des Hauptprogramms
\item[2.15] String-Operationen
\end{itemize}
\item[\textbf{3}] \textbf{Bibliotheken}
\vspace*{-\smallskipamount}
\item[\textbf{\dots}]
% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung}
% \item[\textbf{5}] \textbf{Algorithmen}
% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke}
\end{itemize}
\end{frame}
\end{document}