\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 15}
\date{September 2, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of the previous lecture}
  \begin{itemize}
    \pause
  \item A pointer is an abstraction of memory address.
    \pause
  \item To declare use \text{\tt T~\char42{}ptr\char59{}}
    \pause
  \item A pointer to type \text{\tt T} has as value an address
    of a memory cell capable of storing a value of type \text{\tt T}.
    \pause
  \item The expression \text{\tt \char42{}ptr} is the value stored at the
    location pointed by \text{\tt ptr}.
    \text{\tt \char42{}ptr} is also an l-value and can be assigned.
    \pause
  \item For a variable \text{\tt x}, \text{\tt \char38{}x} is the address of the
    variable. The expression \text{\tt \char38{}x} is \emph{not} an l-value.
  \end{itemize}
    
\end{frame}

\begin{frame}
  \frametitle{Pointer arithmetic}
  \begin{tikzpicture}[]
    \begin{scope}
      [every node/.style={box,draw=black}, node distance=0pt];
      \node (ami) {$a_{-i}$};
      \node [right=of ami] (amdots) {$\ldots$};
      \node [right=of amdots] (am1) {$a_{-1}$};
      \node [right=of am1] (a0) {$a_0$};  
      \node[right=of a0] (a1) {$a_1$};
      %\node[right=of a1] (a2) {$a_2$};
      \node[right=of a1] (ldots){$\ldots$};
      \node[right=of ldots] (ai){$a_i$};
    \end{scope}
    \uncover<6->{
      \node[below =of ami] (ptrmi) {$\texttt{ptr - i}$};
      \draw [->, in=270, out=90] (ptrmi) to (ami);
    }
    \begin{scope}
      [node distance=0, usualedge/.style={->, in=270, out=0}];
      \uncover<6->{
        \node[below =of ptrmi] (ptrmdots) {$\ldots$};
      }
      \uncover<5->{
        \node[below =of ptrmdots] (ptrm1) {$\texttt{ptr - 1}$};
        \draw [usualedge] (ptrm1) to (am1);
      }
     
      \node [box,below=of ptrm1, label= left:$\mathtt{ptr}$] (ptr){$\cdot$};
      \draw [usualedge] (ptr.mid) to (a0);

      \uncover<2->{
        \node [below =of ptr] (ptr1){$\texttt{ptr + 1}$};
        \draw [usualedge] (ptr1) to (a1);
      }
      \uncover<3->{
        \node [below =of ptr1] (ptrdots) {$\ldots$};
        \node [below =of ptrdots] (ptri){$\texttt{ptr + i}$};
        \draw [usualedge]  (ptri) to (ai);
      }
    \end{scope}
  \end{tikzpicture}
\end{frame}
\begin{frame}
  \frametitle{What operations are allowed on pointers?}
  \begin{itemize}
  \item One can add any integer to a pointer of any type 
    \text{\tt ptr~\char43{}~i} is the pointer to the ith location starting from \text{\tt ptr}.
  \item One can subtracte any integer to a pointer of any type
  \item If \text{\tt ptr1} and \text{\tt ptr2} are pointers to the same type then \text{\tt ptr1~\char45{}~ptr2}.
    It is the number of cells between \text{\tt ptr1} and \text{\tt ptr2}.
  \item \text{\tt ptr\char43{}\char43{}}, \text{\tt ptr\char45{}\char45{}} etc makes sense because \text{\tt ptr~\char43{}1} and \text{\tt ptr~\char45{}1} makes sense
  \end{itemize}
  \input{incr}
\end{frame}
  
\begin{frame}
  \frametitle{Relation with arrays}

  Consider
  \begin{tabbing}\tt
~~~~~int~a\char91{}100\char93{}\char59{}
\end{tabbing}

  \begin{itemize}
    \pause
  \item The value \text{\tt a} is the address of the first element.
    \pause
  \item The value \text{\tt a~\char43{}~i} is equivalent to \text{\tt \char38{}a\char91{}i\char93{}}
    \pause
  \item More interestingly \text{\tt a\char91{}i\char93{}} is same as \text{\tt \char42{}\char40{}a\char43{}i\char41{}}
    \pause
  \end{itemize}
  \begin{tabbing}\tt
~~~~~int~a\char91{}100\char93{}\char44{}~\char42{}ptr\char59{}\\
\tt ~~~~~for\char40{}ptr\char61{}a\char59{}~ptr~\char45{}~a~\char60{}~100\char59{}~ptr\char43{}\char43{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~\char42{}ptr~\char61{}~0\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}

    
\end{frame}
\end{document}

\begin{frame}
  \frametitle{Passing arrays to functions}
  
  \begin{itemize}
  \item The value of an array variable is the starting address.
  \item Functions are call by value.
  \end{itemize}

\end{frame}
\begin{frame}
  \begin{tabbing}\tt
~~~~~\char35{}include~\char60{}stdio\char46{}h\char62{}\\
\tt ~\\
\tt ~~~~~int~strlen\char40{}char~\char42{}ptr\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~char~\char42{}start~\char61{}~ptr\char59{}\\
\tt ~~~~~~~while\char40{}\char42{}ptr\char41{}~ptr\char43{}\char43{}\char59{}\\
\tt ~~~~~~~return~ptr~\char45{}~start\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~\\
\tt ~~~~~int~strlen\char40{}char~\char42{}ptr\char41{}\char59{}\\
\tt ~\\
\tt ~~~~~int~main\char40{}\char41{}\char123{}\\
\tt ~~~~~~~char~a\char91{}\char93{}~\char61{}~\char34{}hello~world\char34{}\char59{}\\
\tt ~~~~~~~printf\char40{}\char34{}length~is~\char37{}d\char92{}n\char34{}\char44{}~strlen\char40{}a\char41{}\char41{}\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}


\end{document}
