\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains,fit} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \usepackage{algorithm2e} \usepackage{pgfkeys} \pgfkeys {% /tikz/redbox/.style={% shape=rectangle,% minimum size=1.5em,% top color = white, % bottom color=red!50!black!50, inner sep=0pt, draw=black }, /tikz/greybox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black!20, inner sep=0pt, draw=black }, /tikz/blackbox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black, inner sep=0pt, draw=black }, /tikz/whitebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, draw=black }, /tikz/bluebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, fill=blue!40, draw=black }, /tikz/whitetape/.style={ shape=rectangle, minimum width=4em, minimum height=1.5em, draw=black }, /tikz/bluearr/.style={ ->, line width=0.25em, draw=blue!40 }, /tikz/greenarr/.style={ ->, line width=0.25em, draw=green!40!black!60 } } \newcommand{\arraythree}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1, right=of a0#3](a1#3){#2}; \node[#1, right=of a1#3](a1#3){#2}; \end{tikzpicture} } \newcommand{\arraytwo}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1,right=of a0#3]{#2}; \end{tikzpicture} } \newcommand{\Array}[0]{ \arraythree{box}{}{a}{1} } \newcommand{\AArray}[0]{% \arraythree{bbox}{\Array}{b}{0.5}} \newcommand{\AAArray}[0]{% \arraytwo{bbbox}{\AArray}{c}{0.25}} \newcommand{\T}[1]{\ensuremath{T\left(#1\right)}} \newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}} \title{Fundamentals of Computing: Lecture 23} \date{September 23, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of last class} \pause \begin{enumerate} \item Allocating memory using the |malloc|, \pause \item Freeing memory using |free|, \pause \item Finding size of a type using |sizeof|, \pause \item Finding the length of an array |sizeof(a)/sizeof(a[0])| \end{enumerate} \end{frame} \begin{frame} \frametitle{Void pointers} \pause Void pointers are ``generic'' pointer, \pause \begin{itemize} \item Any pointer can be cast to a void pointer variable \pause \begin{spec} void *vptr; char *str = "hello"; vprt = (void *) str; \end{spec} \pause \item If a void pointer varaible was assigend a pointer to type |T| then a pointer to |T| variable can be cast back from the void pointer. \pause \begin{spec} void *vptr; T t, *tptr; vptr = (void *) &t; ... tptr = (T *) vptr; \end{spec} \pause \item A void pointer cannot be dereference. Nor can pointer arithmetic be done on it. Reason dereferencing does not make sense and for arithmetic we need to know the size. \end{itemize} \end{frame} \end{document} \begin{frame} \frametitle{Structures} \pause \begin{code} # line 170 "lecture23.lhs" struct Vector2D{ double x; double y; }; struct Vector2D origin = {0.0,0.0}; \end{code} This declares that Point2D is a combination of 2 doubles. \pause \begin{block}{Accessing the fields} \begin{spec} struct Vector v; printf("v is the vector (%f,%f)", p.x, p.y ); \end{spec} \end{block} \end{frame} \begin{frame} \frametitle{Fields are both l-values and r-values} \pause \begin{spec} Vector2D addVector(Vector2D u, Vector2D) { Vector2D w; w.x = u.x + v.x; w.y = u.y + v.y; return w; } \end{spec} \end{frame} \begin{frame} \frametitle{Java programmers please note} Structures are \emph{not} passed as reference to functions. \pause \begin{code} #line 214 "lecture23.lhs" #include void printVector(struct Vector2D); void shiftByOneUnit(struct Vector2D u); int main () { printf("Before shift: ");printVector(origin); shiftByOneUnit(origin); printf("After shift: ");printVector(origin); } void shiftByOneUnit(struct Vector2D u){ u.x = u.x + 1; } void printVector(struct Vector2D u){ printf("(%f,%f)\n",u.x,u.y); } \end{code} \end{frame} \begin{frame} \begin{block}{Syntax} \begin{spec} struct name { declarations; /* field declarations */ }; \end{spec} \end{block} \begin{itemize} \item There should be no recursive definition, however \pause \item A structure Foo can have as field member a pointer to Foo. \end{itemize} \end{frame} \end{document} \begin{frame} \frametitle{Abstract data type List} \pause A list is either \begin{itemize} \item An empty list or \pause \item A starting element, called the head of the list, followed by a list of integers, called the tail. \end{itemize} \pause \begin{spec} data List = EmptyList | Node Int List \end{spec} \pause Things are not so clean in |C| \pause \begin{spec} typedef struct Node Node; struct Node { int datum; struct Node *next; }; typedef Node * List; Node emptyNode = {0, NULL}; List emptyList = &emptyNode; \end{spec} \end{frame}