summaryrefslogtreecommitdiff
path: root/Practical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb')
-rwxr-xr-xPractical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb526
1 files changed, 526 insertions, 0 deletions
diff --git a/Practical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb b/Practical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb
new file mode 100755
index 00000000..e3f18fc3
--- /dev/null
+++ b/Practical_C_Programming_by_Steve_Oualline/Chapter_13_1.ipynb
@@ -0,0 +1,526 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:e550d25cd0a5a3cbfd731b0cd1d4a1ad11e5182b8955b4cee863068b4cfdaf40"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 13: Simple pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.1, Page number: 225"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "thing_var = 2\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Thing %d' % thing_var)\n",
+ "\n",
+ "thing_ptr = thing_var\n",
+ "thing_ptr = 3\n",
+ "print ('Thing %d' % thing_ptr)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thing 2\n",
+ "Thing 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.2, Page number: 227"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration\n",
+ "def inc_count (count_ptr) :\n",
+ " count_ptr += 1\n",
+ " return count_ptr\n",
+ "\n",
+ "# Calculation and result\n",
+ "count = 0\n",
+ "\n",
+ "while (count < 10) :\n",
+ " print ('Count %d\\n' % inc_count (count))\n",
+ " count += 1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Count 1\n",
+ "\n",
+ "Count 2\n",
+ "\n",
+ "Count 3\n",
+ "\n",
+ "Count 4\n",
+ "\n",
+ "Count 5\n",
+ "\n",
+ "Count 6\n",
+ "\n",
+ "Count 7\n",
+ "\n",
+ "Count 8\n",
+ "\n",
+ "Count 9\n",
+ "\n",
+ "Count 10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.3, Page number: 230"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('&array[index] (array+index) array[index]\\n')\n",
+ "\n",
+ "for index in range (0, 10) :\n",
+ " print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "&array[index] (array+index) array[index]\n",
+ "\n",
+ "0x20916008 0x20916008 0x0\n",
+ "\n",
+ "0x20270280 0x20270280 0x1\n",
+ "\n",
+ "0x20733728 0x20733728 0x2\n",
+ "\n",
+ "0x20916464 0x20916464 0x3\n",
+ "\n",
+ "0x20270232 0x20270232 0x4\n",
+ "\n",
+ "0x20733560 0x20733560 0x5\n",
+ "\n",
+ "0x20270256 0x20270256 0x6\n",
+ "\n",
+ "0x20733752 0x20733752 0x7\n",
+ "\n",
+ "0x20916512 0x20916512 0x8\n",
+ "\n",
+ "0x20916032 0x20916032 0x9\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.4, Page number: 232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n",
+ "index = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (array[index] != 0) :\n",
+ " index += 1\n",
+ "\n",
+ "print ('Number of elements before zero %d\\n' % index)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of elements before zero 6\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.5, Page number: 232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n",
+ "index = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (array[index] != 0) :\n",
+ " index += 1\n",
+ "\n",
+ "print ('Number of elements before zero %d\\n' % index)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of elements before zero 6\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.6, Page number: 233"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "data = []\n",
+ "\n",
+ "# Calculation and result\n",
+ "for index in range (0, 10) :\n",
+ " data.append(0)\n",
+ "print (data)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.10, Page number: 238"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "line = 'Steve/Oualline'\n",
+ "\n",
+ "# Calculation and result\n",
+ "first_ptr, last_ptr = line.split('/')\n",
+ "print ('First: %s Last: %s\\n' % (first_ptr, last_ptr))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First: Steve Last: Oualline\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.11, Page number: 240"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def tmp_name() :\n",
+ " if not hasattr (tmp_name, 'sequence') :\n",
+ " tmp_name.sequence = 0\n",
+ " tmp_name.sequence += 1\n",
+ " name = 'tmp'\n",
+ " name += str (tmp_name.sequence)\n",
+ " return name\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Name: %s\\n' % tmp_name())"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name: tmp1\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.12, Page number: 245"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "# Variable and function declaration\n",
+ "import sys\n",
+ "v_count = sys.argv\n",
+ "counter = len(sys.argv)\n",
+ "\n",
+ "# Produces verbose messages\n",
+ "verbose = 0\n",
+ "\n",
+ "# Sends output to a file\n",
+ "out_file = open ('print.out', 'w')\n",
+ "\n",
+ "# Sets the number of lines per page\n",
+ "line_max = 66\n",
+ "\n",
+ "def do_file (name) :\n",
+ " print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n",
+ "\n",
+ "def usage() :\n",
+ " print ('Usage is %s [options] [file-list]\\n' % program_name)\n",
+ " print ('Options\\n')\n",
+ " print (' -v verbose\\n')\n",
+ " print (' -l<number> Number of lines\\n')\n",
+ " print (' -o<name> Set output filename\\n')\n",
+ " sys.exit(1)\n",
+ " \n",
+ "# Calculation and result\n",
+ "program_name = str (sys.argv[0])\n",
+ "\n",
+ "while ((counter > 1) and (sys.argv[1][0] == '-')) :\n",
+ " if (sys.argv[1][1] == 'v') :\n",
+ " verbose = 1\n",
+ " break\n",
+ "\n",
+ " elif (sys.argv[1][1] == 'o') :\n",
+ " temp = str (sys.argv[1])\n",
+ " out_file.write (temp)\n",
+ " break\n",
+ "\n",
+ " elif (sys.argv[1][1] == 'l') :\n",
+ " line_max = int (sys.argv[1][2])\n",
+ " break\n",
+ "\n",
+ " else :\n",
+ " print ('Bad option %s\\n' % sys.argv[1])\n",
+ " usage()\n",
+ "\n",
+ " for index in range (0, counter) :\n",
+ " if (index == counter - 1) :\n",
+ " break\n",
+ " else :\n",
+ " v_count[index] = v_count[index + 1]\n",
+ "\n",
+ " counter -= 1\n",
+ "\n",
+ "if (counter == 1) :\n",
+ " do_file ('print.in')\n",
+ "\n",
+ "else :\n",
+ " while (counter > 1) :\n",
+ " do_file (sys.argv[1])\n",
+ "\n",
+ " for index in range (0, counter) :\n",
+ " if (index == counter - 1) :\n",
+ " break\n",
+ " else :\n",
+ " v_count[index] = v_count[index + 1]\n",
+ " \n",
+ " counter -= 1\n",
+ "\n",
+ "out_file.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "SystemExit",
+ "evalue": "1",
+ "output_type": "pyerr",
+ "traceback": [
+ "An exception has occurred, use %tb to see the full traceback.\n",
+ "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Bad option -f\n",
+ "\n",
+ "Usage is -c [options] [file-list]\n",
+ "\n",
+ "Options\n",
+ "\n",
+ " -v verbose\n",
+ "\n",
+ " -l<number> Number of lines\n",
+ "\n",
+ " -o<name> Set output filename\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "To exit: use 'exit', 'quit', or Ctrl-D."
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 13.13, Page number: 248"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration\n",
+ "def tmp_name() :\n",
+ " if not hasattr (tmp_name, 'sequence') :\n",
+ " tmp_name.sequence = 0\n",
+ " tmp_name.sequence += 1\n",
+ " name = 'tmp'\n",
+ " name += str (tmp_name.sequence)\n",
+ " return name\n",
+ "\n",
+ "# Calculation and result\n",
+ "name1 = tmp_name()\n",
+ "name2 = tmp_name()\n",
+ "print ('Name1: %s\\n' % name1)\n",
+ "print ('Name2: %s\\n' % name2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name1: tmp1\n",
+ "\n",
+ "Name2: tmp2\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file