
-----------------------------------
kalev
Mon Apr 06, 2009 6:08 pm

A Guide to Pointers in Turing
-----------------------------------
Pointers are an extremely powerful programming tool. The Turing language makes it much more difficult to use pointers than other languages.  The purpose of this Tutorial is to teach you, the reader and avid beginner programmer, how to use pointers in Turing, just like in higher level languages such as C++ or Java.


  What is a pointer?

  To begin with, you must understand what a pointer is.  To understand what a pointer is, you must understand what a variable is, and how computers interact with them.  Every variable is a bunch of memory stored at a place in the computer's memory. Just like your home address, each variable also has an address.  A pointer is a variable that stores the address of another variable. When a pointer stores an address of a variable, it is said that it points to that variable.  The variable a pointer points to is refered as the variable pointed to by (pointer name).

  Unfortunately, the Turing language makes it easy to confuse objects, and true pointers.  Turing also make it difficult to use "true pointers".  To quote the Turing documentation, pointers "are inherently error prone and cause difficult to locate bugs".  However, pointers are not impossible to use in Turing.  The following example shows how to use a pointer.


type intPointer : unchecked pointer to int %example type name
%note: short form unchecked ^int

var anInt : int %declares a variable
anInt := 9001

var Pointer : intPointer %declares pointer

Pointer := cheat (intPointer, addr (anInt))
%sets address stored by pointer to be the address of variable
%note: cheat only accepts a type identifier as first parameter

#Pointer := addr (anInt) %short form of the pointing

anInt := 2 %value pointed to by pointer should change as well

put anInt
put ^Pointer %should print same thing as variable



  Feel free to play with the above code.  If i left out something important, or made a spelling/grammer mistake, feel free to let me know and i will gladly edit.

  Note: see Turing help's pointer section for detailed info on using Turing's "normal" pointers (checked by default ones)

-----------------------------------
kalev
Tue Apr 07, 2009 6:46 pm

Example of a use of Pointers in Turing
-----------------------------------
Here's a method of creating a linked list in Turing:



type node :  % a point in the linked list
    record
        value : int
        previous, next : unchecked ^node
    end record

var null : node % variable representing nothing
null.value := 0
#null.previous := addr (null) %no meaning
#null.next := addr (null)

var first : node
first.value := 34
#first.previous := addr (null) %first.previous points to nothing

var last : node
last.value := 9001
#last.previous := addr (first) %connects last to first
#last.next := addr (null) %last.next points to nothing

#first.next := addr (last) %connects first to last


proc addItemToBack (var newNode : node)
    %newNode will be added to the end of the list 

Here's a code demonstrating record incomparability.

type aTypeWithRecords :
    record
        field1, field2 : int
    end record

var aVariable : aTypeWithRecords
var anotherVariable : aTypeWithRecords

aVariable.field1 := 2
aVariable.field2 := 3
anotherVariable.field1 := 2
anotherVariable.field2 := 3

%try it
put aVariable = anotherVariable
%not legal (Turing does not allow the comparison of records)


-----------------------------------
Tyr_God_Of_War
Thu Jun 18, 2009 5:33 pm

Re: A Guide to Pointers in Turing
-----------------------------------
You can use nil instead of your node 'null'


var ptr : ^int
new ptr
^ptr := 2

put ^ptr   %outputs 2

ptr := nil

if ptr = nil then
    put "Oh no, a null pointer"
end if


I think that's a bit easier.

-----------------------------------
apomb
Fri Jun 19, 2009 12:25 am

RE:A Guide to Pointers in Turing
-----------------------------------
but... why?
