{ "metadata": { "name": "", "signature": "sha256:5ac60189d3032c0d9eeb79f16671ba934dceed86c00074ca1744202c056c8dca" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 11: Understanding Pointers" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.1, Page No 178" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=chr\n", "x=int\n", "y=float\n", "\n", "print \"Address=\"+str(id(c))+\" Content=\"+str(c)\n", "print \"Address=\"+str(id(x))+\" Content=\"+str(x)\n", "print \"Address=\"+str(id(y))+\" Content=\"+str(y)\n", "\n", "c='A'\n", "x=7\n", "y=123.45\n", "\n", "print \"Address=\"+str(id(c))+\" Content=\"+str(c)\n", "print \"Address=\"+str(id(x))+\" Content=\"+str(x)\n", "print \"Address=\"+str(id(y))+\" Content=\"+str(y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address=19935400 Content=\n", "Address=505552912 Content=\n", "Address=505547744 Content=\n", "Address=20542336 Content=A\n", "Address=20177304 Content=7\n", "Address=41743224 Content=123.45\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.2, Page No 180" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=chr\n", "x=int\n", "y=float\n", "prt_c=chr\n", "ptr_x=int\n", "ptr_y=float\n", "\n", "c='A'\n", "x=7\n", "y=123.45\n", "\n", "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)\n", "print \"X: Address=\"+str(id(x))+\" Content=\"+str(x)\n", "print \"Y: Address=\"+str(id(y))+\" Content=\"+str(y)\n", "\n", "ptr_c=id(c)\n", "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n", "print \"*ptr_c -> \"+str(c)\n", "\n", "ptr_x=id(x)\n", "print \"ptr_x: Address=\"+str(id(ptr_x))+\" Content=\"+str(ptr_x)\n", "print \"*ptr_x -> \"+str(x)\n", "\n", "ptr_y=id(y)\n", "print \"ptr_y: Address=\"+str(id(ptr_y))+\" Content=\"+str(ptr_y)\n", "print \"*ptr_y -> \"+str(y)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "C: Address=20542336 Content=A\n", "X: Address=20177304 Content=7\n", "Y: Address=41743208 Content=123.45\n", "ptr_c: Address=42208720 Content=20542336\n", "*ptr_c -> A\n", "ptr_x: Address=42206796 Content=20177304\n", "*ptr_x -> 7\n", "ptr_y: Address=42206784 Content=41743208\n", "*ptr_y -> 123.45\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.3, Page No 183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c=chr\n", "ptr_c=chr\n", "\n", "c='A'\n", "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)\n", "\n", "ptr_c=id(c)\n", "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n", "print \"*ptr_c -> \"+str(c)\n", "\n", "ptr_c='B'\n", "print \"ptr_c: Address=\"+str(id(ptr_c))+\" Content=\"+str(ptr_c)\n", "print \"*ptr_c -> \"+str(ptr_c)\n", "\n", "print \"C: Address=\"+str(id(c))+\" Content=\"+str(c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "C: Address=20542336 Content=A\n", "ptr_c: Address=42206808 Content=20542336\n", "*ptr_c -> A\n", "ptr_c: Address=20342424 Content=B\n", "*ptr_c -> B\n", "C: Address=20542336 Content=A\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11.4, Page No 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=1234\n", "print \"X: address=\"+str(id(x))+\" Content=\"+str(x)\n", "\n", "ptr_1=id(x)\n", "print \"ptr_1: Address=\"+str(id(ptr_1))+\" Content=\"+str(ptr_1)\n", "print \"*ptr_1 -> \"+str(ptr_1)\n", "\n", "ptr_2=id(x)\n", "print \"ptr_2: Address=\"+str(id(ptr_2))+\" Content=\"+str(ptr_2)\n", "print \"*ptr_2 -> \"+str(ptr_2)\n", "\n", "ptr_3=ptr_1\n", "print \"ptr_3: Address=\"+str(id(ptr_3))+\" Content=\"+str(ptr_3)\n", "print \"*ptr_3 -> \"+str(ptr_3)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "X: address=42206832 Content=1234\n", "ptr_1: Address=42206928 Content=42206832\n", "*ptr_1 -> 42206832\n", "ptr_2: Address=42206916 Content=42206832\n", "*ptr_2 -> 42206832\n", "ptr_3: Address=42206928 Content=42206832\n", "*ptr_3 -> 42206832\n" ] } ], "prompt_number": 5 } ], "metadata": {} } ] }