home download demos platforms restrictions help

 

What is an address operator and pointer dereferencing?

A very powerful facility within the C programming language is the ability to manipulate the memory pointers as though they are data (just like any other number). XCSB also supports memory pointers similar to those of C. A very useful ability within C is the ability to quickly and easily determin the address of a variable and assign it to a pointer so that the variable can subsequently be manipulated indirectly.

The XCSB address of operator is the & (ampersand character).

The XCSB pointer dereference operator is the * (asterisk character).

X = &Y    means assign the address of Y to the variable X
W = *X    means assign the value at the location pointed to by X to the variable W (in this case X points to Y, so indirectly access the contents of Y and assign it to W)

simple example showing the use of pointers and addresses

Non-pointer version

    if a < b then
        if x < y then
            if a < x then    
                a = a + 1
            else
                x = x + 1
            endif
        else
            if a < y then
                a = a + 1
            else
                y = y + 1
            endif
        endif
    else
        if x < y then
            if b < x then
                b = b + 1
            else
                x = x + 1
            endif
        else
            if b < y then
                b = b + 1
            else
                y = y + 1
            endif
        endif
    endif
Pointer version

    if a < b then
        s1 = &a
    else
        s1 = &b
    endif

    if x < y then
        s2 = &x
    else
        s2 = &y
    endif

    if *s1 >= *s2 then    
        s1 = s2
    endif

    *s1 = *s1 + 1

Why does XCSB not need PEEK and POKE functions?

Since XCSB has pointer capabilities it is not necessary to have the PEEK and POKE functions necessary in other versions of BASIC. By simply using the address directly as a pointer, XCSB achives the functionality of both PEEK and POKE and whereas use of PEEK and POKE would interfere with optimisation, using pointers does not have this drawback.

simple example showing the use of pointers in place of PEEK and POKE

Using PEEK and POKE
	POKE(123, PEEK(123)+1)        
Using pointers
	*123 = (*123) + 1        


 
home download demos platforms restrictions help