\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 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 |T *ptr;| \pause \item A pointer to type |T| has as value an address of a memory cell capable of storing a value of type |T|. \pause \item The expression |*ptr| is the value stored at the location pointed by |ptr|. |*ptr| is also an l-value and can be assigned. \pause \item For a variable |x|, |&x| is the address of the variable. The expression |&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 |ptr + i| is the pointer to the ith location starting from |ptr|. \item One can subtracte any integer to a pointer of any type \item If |ptr1| and |ptr2| are pointers to the same type then |ptr1 - ptr2|. It is the number of cells between |ptr1| and |ptr2|. \item |ptr++|, |ptr--| etc makes sense because |ptr +1| and |ptr -1| makes sense \end{itemize} \input{incr} \end{frame} \begin{frame} \frametitle{Relation with arrays} Consider \begin{spec} int a[100]; \end{spec} \begin{itemize} \pause \item The value |a| is the address of the first element. \pause \item The value |a + i| is equivalent to |&a[i]| \pause \item More interestingly |a[i]| is same as |*(a+i)| \pause \end{itemize} \begin{spec} int a[100], *ptr; for(ptr=a; ptr - a < 100; ptr++) { *ptr = 0; } \end{spec} \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{code} #include int strlen(char *ptr) { char *start = ptr; while(*ptr) ptr++; return ptr - start; } int strlen(char *ptr); int main(){ char a[] = "hello world"; printf("length is %d\n", strlen(a)); } \end{code} \end{frame} \end{document}