Pointers and references: Difference between revisions

Content added Content deleted
(→‎{{header|Racket}}: re-add in the right place)
m (Added syntax highlighting to C and C++, C++ and Delphi examples.)
Line 135: Line 135:
=={{header|C}} and {{header|C++}}==
=={{header|C}} and {{header|C++}}==
The following code creates a pointer to an int variable
The following code creates a pointer to an int variable
int var = 3;
<lang c>int var = 3;
int *pointer = &var;
int *pointer = &var;</lang>

Access the integer variable through the pointer:
Access the integer variable through the pointer:
int v = *pointer; /* sets v to the value of var (i.e. 3) */
<lang c>int v = *pointer; /* sets v to the value of var (i.e. 3) */
*pointer = 42; /* sets var to 42 */
*pointer = 42; /* sets var to 42 */</lang>

Change the pointer to refer to another object
Change the pointer to refer to another object
int othervar;
<lang c>int othervar;
pointer = &othervar;
pointer = &othervar;</lang>

Change the pointer to not point to any object
Change the pointer to not point to any object
pointer = NULL; /* needs having stddef.h included */
<lang c>pointer = NULL; /* needs having stddef.h included */</lang>
or
or
pointer = 0; /* actually any constant integer expression evaluating to 0 could be used, e.g. (1-1) will work as well */
<lang c>pointer = 0; /* actually any constant integer expression evaluating to 0 could be used, e.g. (1-1) will work as well */</lang>
or
or
pointer = (void*)0; /* C only, not allowed in C++ */
<lang c>pointer = (void*)0; /* C only, not allowed in C++ */</lang>

Get a pointer to the first element of an array:
Get a pointer to the first element of an array:
int array[10];
<lang c>int array[10];
pointer = array;
pointer = array;
/* or alternatively: */
/* or alternatively: */
pointer = &array[0];
pointer = &array[0];</lang>

Move the pointer to another object in the array
Move the pointer to another object in the array
pointer += 3; /* pointer now points to array[3] */
<lang c>pointer += 3; /* pointer now points to array[3] */
pointer -= 2; /* pointer now points to array[1] */
pointer -= 2; /* pointer now points to array[1] */</lang>

Access another object in the same array through the pointer
Access another object in the same array through the pointer
v = pointer[3]; /* accesses third-next object, i.e. array[4] */
<lang c>v = pointer[3]; /* accesses third-next object, i.e. array[4] */
v = pointer[-1]; /* accesses previous object, i.e. array[0] */
v = pointer[-1]; /* accesses previous object, i.e. array[0] */
/* or alternatively */
/* or alternatively */
v = *(pointer + 3); /* array[4] */
v = *(pointer + 3); /* array[4] */
v = *(pointer - 1); /* array[0] */
v = *(pointer - 1); /* array[0] */</lang>

=={{header|C++}}==

'''With pointers:'''
See 'C' example above.

C++ specific alternative to "The following code creates a pointer to an int variable":
<lang cpp>int* pointer2(&var);</lang>

'''With references:'''

The following code create a reference to an int variable:
<lang cpp>int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);</lang>

Access the integer variable through the reference
<lang cpp>int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42</lang>

References cannot be changed to refer to other objects, and cannot (legally) made to refer to no object.

Get a reference to the first element of an array:
<lang cpp>int array[10];
int& ref3 = array[0];</lang>

Changing the reference to refer to another object of the array is not possible.

Accessing another object of the array through the reference:
<lang cpp>v = (&ref)[3]; // read value of array[3]; however doing this is bad style</lang>


=={{header|c sharp|C#}}==
=={{header|c sharp|C#}}==
Line 201: Line 238:
Val After : 1
Val After : 1
</pre>
</pre>

=={{header|C++}}==

'''With pointers:'''
See 'C' example above.

C++ specific alternative to "The following code creates a pointer to an int variable":
int* pointer2(&var);

'''With references:'''

The following code create a reference to an int variable:
int var = 3;
int& ref = var;
// or alternatively:
int& ref2(var);
Access the integer variable through the reference
int v = ref; // sets v to the value of var, that is, 3
ref = 42; // sets var to 42
References cannot be changed to refer to other objects, and cannot (legally) made to refer to no object.

Get a reference to the first element of an array:
int array[10];
int& ref3 = array[0];
Changing the reference to refer to another object of the array is not possible.

Accessing another object of the array through the reference:
v = (&ref)[3]; // read value of array[3]; however doing this is bad style


{{omit from|Clojure}}
{{omit from|Clojure}}
Line 269: Line 278:
Variable Declaration
Variable Declaration


pMyPointer : Pointer ;
<lang delphi>pMyPointer : Pointer ;</lang>


Simple pointer to a ''predefined'' type:
Simple pointer to a ''predefined'' type:
Line 275: Line 284:
Variable Declaration
Variable Declaration


pIntPointer : ^Integer ;
<lang delphi>pIntPointer : ^Integer ;</lang>


A pointer to a ''Record''. This is the equivalent to a ''Struct'' in C
A pointer to a ''Record''. This is the equivalent to a ''Struct'' in C
Line 281: Line 290:
Type Defintion
Type Defintion


MyRecord = Record
<lang delphi>MyRecord = Record
FName : string[20];
FName : string[20];
LName : string[20];
LName : string[20];
end;
end;</lang>


Variable Declaration
Variable Declaration


pMyRecord : ^MyRecord ;
<lang delphi>pMyRecord : ^MyRecord ;</lang>


Note that when defining a pointer type, unless otherwise, you may refer to the pointed to type before defining it. For example the following is legal despite tFoo not being defined at the pointer definition:
Note that when defining a pointer type, unless otherwise, you may refer to the pointed to type before defining it. For example the following is legal despite tFoo not being defined at the pointer definition:

type
<lang delphi>type
pFoo = ^tFoo; { allowed despite tFoo not yet being defined }
pFoo = ^tFoo; { allowed despite tFoo not yet being defined }
tFoo = record
tFoo = record
value1, value2: integer;
end;
value1, value2: integer;
end;</lang>


- Dereferencing a Pointer -
- Dereferencing a Pointer -
Line 301: Line 311:
Pointers are dereferenced using the caret '^' symbol. Dereferencing will cause the compiler or RTL to reveal the data that the pointer points to:
Pointers are dereferenced using the caret '^' symbol. Dereferencing will cause the compiler or RTL to reveal the data that the pointer points to:


IntVar := pIntPointer^ ;
<lang delphi>IntVar := pIntPointer^ ;</lang>


Dereference with a type cast of an ''untyped'' pointer. It is not legal syntax to simply dereference an untyped pointer, since there is nothing to designate its type. It must therefor be "Type Cast" when the dereference takes place as below.
Dereference with a type cast of an ''untyped'' pointer. It is not legal syntax to simply dereference an untyped pointer, since there is nothing to designate its type. It must therefor be "Type Cast" when the dereference takes place as below.


IntVar := integer(MyPointer^);
<lang delphi>IntVar := integer(MyPointer^);</lang>


- Pushing a Pointer -
- Pushing a Pointer -
Line 311: Line 321:
Many programmers are familiar with C's ability to increment pointers of any type by using the '''++''' or '''--''' operators. The equivalent in Delphi is to use either the Inc or Dec standard procedure:
Many programmers are familiar with C's ability to increment pointers of any type by using the '''++''' or '''--''' operators. The equivalent in Delphi is to use either the Inc or Dec standard procedure:


Inc(PtrVar); //increment by one element
<lang delphi>Inc(PtrVar); //increment by one element
Inc(PtrVar, 4); //incremement by four elements
Inc(PtrVar, 4); //incremement by four elements
Dec(PtrVar); //decrement by one element
Dec(PtrVar); //decrement by one element</lang>


C-style array indexing is also supported by default for PByte (a pointer to a byte), PAnsiChar (a pointer to a singlebyte character) and PWideChar (a pointer to a doublebyte character). For any other typed pointer, it can be enabled using a compiler directive:
C-style array indexing is also supported by default for PByte (a pointer to a byte), PAnsiChar (a pointer to a singlebyte character) and PWideChar (a pointer to a doublebyte character). For any other typed pointer, it can be enabled using a compiler directive:


{$POINTERMATH ON}
<lang delphi>{$POINTERMATH ON}
PrevIntVar := MyIntPtr[-1];
PrevIntVar := MyIntPtr[-1];
Rec4 := MyRecPtr[4];
Rec4 := MyRecPtr[4];</lang>


=={{header|E}}==
=={{header|E}}==