Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Guide to Pointers in Turing
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kalev




PostPosted: Mon Apr 06, 2009 6:08 pm   Post subject: 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.

Turing:

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)
Sponsor
Sponsor
Sponsor
sponsor
kalev




PostPosted: Tue Apr 07, 2009 6:46 pm   Post subject: Example of a use of Pointers in Turing

Here's a method of creating a linked list in Turing:


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 [ will become the last in the list ]
    var tempNode : node := newNode %store value

    newNode := last %the last node will no longer be the last
    %newNode will hold the value of the old last node

    last.value := tempNode.value %new last node will hold the value of the old newNode

    #newNode.next := addr (last) %link the new last node to the old last node
    #last.previous := addr (newNode)
end addItemToBack

proc listAll
    var current : unchecked ^node
    #current := addr (first) %point current to the first item in the list
    loop
        exit when #current = addr (null)
        %^current = null cannot be used because Turing does not allow the comparison of records
        %cheats [#] must be used whenever accessing the value of a pointer, even in comparisons

        put ^current.value
        current := ^current.next
    end loop
end listAll

%note: this works with both unchecked pointers and checked pointers

%note2: there are much better, easier, simpler, and less confusing
% methods of creating linked lists, mainly flexible arrays


Here's a code demonstrating record incomparability.
Turing:

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




PostPosted: Thu Jun 18, 2009 5:33 pm   Post subject: Re: A Guide to Pointers in Turing

You can use nil instead of your node 'null'

Turing:

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




PostPosted: Fri Jun 19, 2009 12:25 am   Post subject: RE:A Guide to Pointers in Turing

but... why?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: