{
 "metadata": {
  "name": "",
  "signature": "sha256:b382733cd221154cfe7c9ffe63477448084168d501d248c2b1d0c253ed011821"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 6: Pointers<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.1, Page Number: 107<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "balance =c_int(3200)       #int variable\n",
      "balptr=pointer(balance)    #pointer to int\n",
      "value=balptr[0]            #accessing the value using the pointer\n",
      "\n",
      "#Result\n",
      "print \"balance: \",value    \n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "balance:  3200\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.2, Page Number: 109<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "#Variable declaration\n",
      "x=123.23\n",
      "y=c_double()\n",
      "p=POINTER(c_int)\n",
      "\n",
      "p=pointer(c_int(int(x)))    #type case double to int\n",
      "y=p[0]\n",
      "\n",
      "#Result\n",
      "print y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "123\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.3, Page Number: 110<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "num=c_int()            #declaring int\n",
      "p=pointer(num)         #pointer to int\n",
      "\n",
      "p[0]=100\n",
      "print num.value,\n",
      "p[0]+=1\n",
      "print num.value,\n",
      "p[0]-=1\n",
      "print num.value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "100 101 100\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.4, Page Number: 111<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "j=c_int()\n",
      "g=c_double()\n",
      "i=pointer(j)\n",
      "f=pointer(g)\n",
      "\n",
      "\n",
      "for x in range(10):\n",
      "    print addressof(i.contents)+x,addressof(f.contents)+x\n",
      "    \n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "84638480 84639248\n",
        "84638481 84639249\n",
        "84638482 84639250\n",
        "84638483 84639251\n",
        "84638484 84639252\n",
        "84638485 84639253\n",
        "84638486 84639254\n",
        "84638487 84639255\n",
        "84638488 84639256\n",
        "84638489 84639257\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.5, Page Number: 114<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "str=\"This is a test\"\n",
      "token =\"\"\n",
      "i=0\n",
      "\n",
      "#Read a token at a time from the string\n",
      "while i<len(str):\n",
      "    token=c_char_p(\"\")    #set token to null string\n",
      "    q=pointer(token)           \n",
      "    #Read characters until either a space or the null terminator is encountered'''\n",
      "    while i<len(str) and not(str[i]==\" \"):\n",
      "        q[0]+=str[i]\n",
      "        i+=1\n",
      "    i+=1                 #advance past the space\n",
      "    print q[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This\n",
        "is\n",
        "a\n",
        "test\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.6, Page Number: 115<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "str=\"This is a test\"\n",
      "i=0\n",
      "\n",
      "#Read a token at a time from the string\n",
      "while i<len(str):\n",
      "    token=\"\"           #set q to null string\n",
      "    #Read characters until either a space or the null terminator is encountered'''\n",
      "    while i<len(str) and not(str[i]==\" \"):\n",
      "        token+=str[i]\n",
      "        i+=1\n",
      "    i+=1           #advance past the space\n",
      "    print token"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This\n",
        "is\n",
        "a\n",
        "test\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.7, Page Number: 115<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import string\n",
      "\n",
      "str=c_char_p(\"hello tom\")\n",
      "q=\"\"\n",
      "p=pointer(str)              #put address of str into p\n",
      "   \n",
      "p[0]=string.upper(p[0])\n",
      "\n",
      "#Result\n",
      "print p[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "HELLO TOM\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.8, Page Number: 117<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "#Varible declaration\n",
      "s=\"Pointers are fun to use\"\n",
      "\n",
      "#Result\n",
      "print s\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Pointers are fun to use\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.9, Page Number: 118<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "num=[]\n",
      "\n",
      "#User input\n",
      "for i in range(10):\n",
      "    num.append(i)\n",
      "    \n",
      "start=num          #set start to the starting pointer \n",
      "for i in range(10):\n",
      "    print start[i],\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0 1 2 3 4 5 6 7 8 9\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.10, Page Number: 119<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import random\n",
      "\n",
      "fortunes=[\"Soon, you will come into some money.\",\n",
      "          \"A new love will enter your lifr. \",\n",
      "          \"You will live long and prosper.\",\n",
      "          \"Now is a good time to invest for the future.\",\n",
      "          \"A close friend will ask for a favour.\"]\n",
      "\n",
      "print \"To see your fortune, press a key: \"\n",
      "\n",
      "#Randomize the random number generator\n",
      "chance=random.randint(0,100)\n",
      "chance=chance%5\n",
      "\n",
      "#Result\n",
      "print fortunes[chance]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "To see your fortune, press a key: \n",
        "A close friend will ask for a favour.\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.11, Page Number: 120<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "keyword=[[\"for\",\"for(initialization; condition; increment\"],\n",
      "         [\"if\",\"if(condition) ... else ...\"],\n",
      "         [\"switch\",\"switch(value) { case-list }\"],\n",
      "         [\"while\",\"while(condition) ...\"],\n",
      "         [\"\",\"\"]]     #Terminates the list with nulls\n",
      "          \n",
      "#User input      \n",
      "print \"Enter keyword: \"\n",
      "str=\"for\"              \n",
      "\n",
      "for i in range(4):\n",
      "    if str==keyword[i][0]:\n",
      "        print keyword[i][1]\n",
      "        \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter keyword: \n",
        "for(initialization; condition; increment\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.12, Page Number: 123<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "x=c_int(10)          #int variable\n",
      "p=pointer(x)         #pointer to int\n",
      "q=pointer(p)         #pointer to a pointer\n",
      "\n",
      "#Result\n",
      "print q[0][0]        #accessing the value using a pointer to a pointer\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 6.13, Page Number: 126<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      "#Variable declaration\n",
      "s=c_char_p()\n",
      "p1=pointer(s)\n",
      "x=0\n",
      "\n",
      "while True:\n",
      "    print \"\\nEnter a string: \",\n",
      "    if x==2:\n",
      "        p1[0]=c_char_p(\"done\")\n",
      "    else:\n",
      "        p1[0]=c_char_p(\"Hello\")\n",
      "    #print the ASCII values of each characcter\n",
      "    for i in range(0,len(p1[0])):\n",
      "        print ord(p1[0][i]),\n",
      "    x+=1\n",
      "    if p1[0]==\"done\":\n",
      "        break\n",
      "        \n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "Enter a string:  72 101 108 108 111 \n",
        "Enter a string:  72 101 108 108 111 \n",
        "Enter a string:  100 111 110 101\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}