Jump to content

Making a Programming Language From Scratch/Pointers

From Wikibooks, open books for an open world

Introduction to pointers

[edit | edit source]

Pointers are variables that store the addresses of other variables. This variable is generally a DWORD or a 32-bit integer, however the variable the address of which the pointer is storing can be of any type. Pointers are used extensively to pass static or local variables to be edited and are the major type of arguments provided to a void function.

Pointer Format

[edit | edit source]

The following is the traditional C and C++ format to declare pointer variables.

[type of variable of which the pointer is storing value of][*(represents pointer)][variable name][initialization of pointer][,|;]

Note that pointer variables can be declared among the variable type of which it is storing the value. Example:

float f1,f2,*p1,*p2=&f2;

The & operator provides the address of the variable rather than the value stored in it.

Algorithm for declaration

[edit | edit source]

The algorithm for this is as follows:

1. If character be * then go through following steps else skip it.
2. Get name of variable.
3. If next character not be = then write to .data section as [name of variable] ptr [type] ?
4. Else get value. Remove first character as  it will be &. Write to .data? section as [name of variable] ptr [type] [value].
5. Repeat step 1.


Clipboard

To do:
Add referencing algorithm and examples