This wiki is out of date, use the continuation of this wiki instead

Pointer

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 18:48, 17 May 2008 (edit)
85.144.194.29 (Talk)
(Concept - Pointers can only point to strong datatypes, info about how to go about a struct pointer.)
← Previous diff
Revision as of 14:06, 19 May 2008 (edit) (undo)
Sandman (Talk | contribs)

Next diff →
Line 14: Line 14:
=== Concept === === Concept ===
-'''Pointer'''s, are used to point to a location in [[memory]]. It uses 32 [[bit]]s (4 [[byte]]s) so it can map 4GB of memory into bytes. '''Pointer'''s can point to any strong [[datatype]]: [[int]]s, [[short]]s, [[string]]s or even usermade datatypes. In order to point to a weak datatype like a struct, the struct would have to encapsulated in a user defined datatype.+'''Pointer'''s, are used to point to a location in [[memory]]. It uses 32 [[bit]]s (4 [[byte]]s) so it can map 4GB of memory into bytes. '''Pointer'''s can point to any [[datatype]]: [[int]]s, [[short]]s, [[string]]s or even usermade datatypes.
 +However, using a <code>struct pointer my_pointer</code> is pointless, because the compiler has no knowledge of the elements inside the struct pointing to, since it doesn't know which struct is meant. <code>MyStruct pointer my_pointer</code>, where MyStruct is an existing struct, is also not valid, because MyStruct is not a datatype. The only way to have something like a <code>struct pointer my_pointer</code> is to use [[Type]] as seen in the example.
== Example == == Example ==
<pre> <pre>
-Program pointers;+ 
 +Type _point
 + int x;
 + int y;
 +End
 + 
 +Type _person
 + string name;
 + int age;
 +End
 + 
 +Global
 + _person Person;
 +End
 + 
 +Process Main()
Private Private
int my_int; int my_int;
- int pointer my_int_pointer;+ int* my_int_pointer;
 + _point myPoint;
 + _person* personPointer; // possible, because _person is infact a datatype
 + //Person* personPointer; // not possible, because Person is not a datatype
Begin Begin
Line 33: Line 52:
say(my_int); say(my_int);
say(*my_int_pointer); say(*my_int_pointer);
 +
 + setXY(&myPoint);
 + say(myPoint.x);
 + say(myPoint.y);
 +
 + personPointer = &Person;
 + personPointer.name = "Mies";
 + say(Person.name);
 + say(personPointer.name);
Repeat Repeat
Line 38: Line 66:
Until(key(_esc)) Until(key(_esc))
 +End
 +
 +Function int setXY(_point* p)
 +Begin
 + p.x = 3; // this is actually (*p).x = 3, but . can be used like this
 + p.y = 5; // this is actually (*p).y = 5, but . can be used like this
 + return 0;
End End
</pre> </pre>
-The & (offset) operator, when used with pointers, returns a [[void]] pointer to a variable. In the example it returns an int pointer to the variable my_int. The * (pointer) operator, when used with pointers, makes it so the pointer variable is not accessed, but the variable it's pointing to. In the example it changes access from my_int_pointer to my_int.+Used in example: [[say]](), [[key]](), [[Type]], [[Global]], [[Private]], [[point]]
 + 
 +The & (offset) operator, when used with pointers, returns a [[void]] pointer to a variable. In the example it returns an [[int]] pointer to the variable my_int. The * (pointer) operator, when used with pointers, makes it so the pointer variable is not accessed, but the variable it's pointing to. In the example it changes access from my_int_pointer to my_int.

Revision as of 14:06, 19 May 2008


Contents

Definition

Statement

Declaration of a pointer:
<datatype> POINTER <pointername>
<datatype> * <pointername>

Assignment of a value to the location pointed to:
POINTER <pointername> = <value>;
* <pointername> = <value>;

Concept

Pointers, are used to point to a location in memory. It uses 32 bits (4 bytes) so it can map 4GB of memory into bytes. Pointers can point to any datatype: ints, shorts, strings or even usermade datatypes. However, using a struct pointer my_pointer is pointless, because the compiler has no knowledge of the elements inside the struct pointing to, since it doesn't know which struct is meant. MyStruct pointer my_pointer, where MyStruct is an existing struct, is also not valid, because MyStruct is not a datatype. The only way to have something like a struct pointer my_pointer is to use Type as seen in the example.

Example


Type _point
    int x;
    int y;
End

Type _person
    string name;
    int age;
End

Global
    _person Person;
End

Process Main()
Private
    int my_int;
    int* my_int_pointer;
    _point myPoint;
    _person* personPointer; // possible, because _person is infact a datatype
    //Person* personPointer; // not possible, because Person is not a datatype
Begin

    my_int_pointer = &my_int;

    my_int = 3;
    say(my_int);
    say(*my_int_pointer);
  
    *my_int_pointer = 4;
    say(my_int);
    say(*my_int_pointer);

    setXY(&myPoint);
    say(myPoint.x);
    say(myPoint.y);

    personPointer = &Person;
    personPointer.name = "Mies";
    say(Person.name);
    say(personPointer.name);

    Repeat
        frame;
    Until(key(_esc))

End

Function int setXY(_point* p)
Begin
    p.x = 3; // this is actually (*p).x = 3, but . can be used like this
    p.y = 5; // this is actually (*p).y = 5, but . can be used like this
    return 0;
End

Used in example: say(), key(), Type, Global, Private, point

The & (offset) operator, when used with pointers, returns a void pointer to a variable. In the example it returns an int pointer to the variable my_int. The * (pointer) operator, when used with pointers, makes it so the pointer variable is not accessed, but the variable it's pointing to. In the example it changes access from my_int_pointer to my_int.

Personal tools