\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} \usepackage{multicol} \pgfkeys % {% /tikz/box/.style={ shape=rectangle,% minimum size=2em,% top color=white, bottom color=red!50!black!50, inner sep=0pt}, /tikz/garbage/.style={% starburst,% fill=yellow,% draw=red } } \title{Fundamentals of Computing: Lecture 14} \date{August 31, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Pointers} \begin{itemize} \pause \item A pointer is an abstraction of memory address. \pause \item Value of a pointer variable of type |T| is an address of a memory cell capable of storing a value of type |T|. \end{itemize} \pause \begin{tikzpicture} \node [style=box] at (0,1) (p) [label=above:$\mathtt{ptr}$]{$\cdot$}; \node [style=box] at (3,0) (x) [label=above:$\mathtt{x}$]{$5$}; \draw [->] (p.mid) to [out=0, in=180] (x.west); \end{tikzpicture} \pause Before we start: \begin{block}{WARNING} Too much of pointer gymnastics can cause serious injury to readability. \end{block} \end{frame} \begin{frame} \frametitle{Declaration} Let |T| be a type then we can declare a pointer to |T| as |T *ptr| \pause \begin{block}{Example} \begin{code} int x, *p, **pp; \end{code} \pause The above code declares \begin{itemize} \item |x| is an integer \item |p| is a pointer to an integer \item |pp| is a pointer to a pointer to an integer. \end{itemize} \end{block} \end{frame} \begin{frame} \begin{block}{Dereferencing} For a pointer variable |ptr|, |*ptr| is the value stored in the location pointed by |ptr|. \end{block} \pause \begin{block}{Conversely} If |x| is a variable then |&x| is the address of the variable. \end{block} \pause \begin{multicols}{2} \uncover<2->{ \begin{code} int x, *p, *pp; \end{code} } \uncover<5->{ \begin{code} x =5; p = &x; pp = &p; \end{code} } \newpage \begin{tikzpicture} \uncover<4->{ \node [garbage] at (2,3) (g){$\bot$}; \node [style=box] at (0,1) (pp) [label=above:$\mathtt{pp}$] {$\cdot$}; \node [style=box] at (2,0) (p) [label=above:$\mathtt{p}$]{$\cdot$}; } \uncover<4-5>{ \node[style=box] at (4,1) (x)[label=above:$\mathtt{x}$]{$\bot$}; \draw [->] (p.mid) to [out=0, in=270] (g.south); \draw [->] (pp.mid) to [out=0, in=180] (g.west); } \uncover<6->{ \node[style=box] at (4,1) (x) [label=above:$\mathtt{x}$]{$5$}; \draw [->] (p.mid) to [out=0, in=180] (x.west); \draw [->] (pp.mid) to [out=0, in=180] (p.west); } \end{tikzpicture} \end{multicols} \end{frame} \begin{frame} \frametitle{Swapping: Pointer version} \begin{code} void swap(int *, int *); int main() { int x=15,y=42; printf("x = %d, y = %d\n",x,y); swap(&x,&y); printf("x = %d, y = %d\n",x,y); } void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } \end{code} \end{frame} \begin{frame} \frametitle{Swapping Pointer version} \begin{tikzpicture} [background rectangle/.style={draw=yellow!50,fill=yellow!10,rounded corners}] %\pgfdeclarelayer{background} %\pgfsetlayers{background,main} \node [style=box] at (0,1) (x) [label=above:$\mathtt{x}$] {$15$}; \node [style=box] at (0,-1) (y) [label=above:$\mathtt{y}$] {$42$}; \pause \node[style=box] at (3,2) (a)[label=above:$\mathtt{a}$] {$\cdot$}; \node[style=box] at (3,0) (t)[label=above:$\mathtt{temp}$]{$\bot$}; \node[style=box] at (3,-2) (b)[label=above:$\mathtt{b}$] {$\cdot$}; \draw [->] (a.mid) to [out=180, in=0] (x.east); \draw [->] (b.mid) to [out=180, in=0] (y.east); \pause \draw [->, line width=0.25em, blue!20] (x.south) to [out=270, in=180] node [black, auto] {$\texttt{temp = *a}$} (t.west); \pause \node[style=box] at (3,0) (t)[label=above:$\mathtt{temp}$]{$15$}; \pause \draw [->, line width=0.25em, blue!20] (y.west) to [out=270, in=180] node [black, auto] {$\texttt{*a = *b}$} (x.west) ; \pause \node[style=box] at (x) [label=above:$\mathtt{x}$]{$42$}; \pause \draw [->, line width=0.25em, blue!20] (t.south) to [out=270, in=0] node [black, above] {$\texttt{*b=temp}$} (y.north east); \pause \node[style=box] at (y) [label=above:$\mathtt{y}$]{$15$}; \end{tikzpicture} \end{frame} \end{document}