\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\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 \text{\tt T} is an address of a
    memory cell capable of storing a value of type \text{\tt 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 \text{\tt T} be a type then we can declare a pointer to
  \text{\tt T} as \text{\tt T~\char42{}ptr}

  \pause
  \begin{block}{Example}
    \begin{tabbing}\tt
~~~~~~~int~x\char44{}~\char42{}p\char44{}~\char42{}\char42{}pp\char59{}
\end{tabbing}

    \pause
    The above code declares 
    \begin{itemize}
    \item \text{\tt x} is an integer
    \item \text{\tt p} is a pointer to an integer
    \item \text{\tt 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 \text{\tt ptr}, \text{\tt \char42{}ptr} is the value stored in the
    location pointed by \text{\tt ptr}.
  \end{block}
  \pause
  \begin{block}{Conversely}
    If \text{\tt x} is a variable then \text{\tt \char38{}x} is the address of the variable.
  \end{block}
  
  \pause

  \begin{multicols}{2}
    \uncover<2->{
      \begin{tabbing}\tt
~~~~~~~~~int~x\char44{}~\char42{}p\char44{}~\char42{}pp\char59{}
\end{tabbing}
    }
    \uncover<5->{
      \begin{tabbing}\tt
~~~~~~~~~x~\char61{}5\char59{}\\
\tt ~~~~~~~~~p~\char61{}~\char38{}x\char59{}\\
\tt ~~~~~~~~~pp~\char61{}~\char38{}p\char59{}
\end{tabbing}
    }
    \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{tabbing}\tt
~~~~~void~swap\char40{}int~\char42{}\char44{}~int~\char42{}\char41{}\char59{}\\
\tt ~~~~~int~main\char40{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~int~x\char61{}15\char44{}y\char61{}42\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}x\char44{}y\char41{}\char59{}\\
\tt ~~~~~~~swap\char40{}\char38{}x\char44{}\char38{}y\char41{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}x~\char61{}~\char37{}d\char44{}~y~\char61{}~\char37{}d\char92{}n\char34{}\char44{}x\char44{}y\char41{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~void~swap\char40{}int~\char42{}a\char44{}~int~\char42{}b\char41{}\\
\tt ~~~~\char123{}\\
\tt ~~~~~~int~temp\char59{}\\
\tt ~~~~~~temp~\char61{}~\char42{}a\char59{}\\
\tt ~~~~~~\char42{}a~\char61{}~\char42{}b\char59{}\\
\tt ~~~~~~\char42{}b~\char61{}~temp\char59{}\\
\tt ~~~~\char125{}
\end{tabbing}
      
\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}
