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 |
||
<lang c>int var = 3; |
|||
int *pointer = &var;</lang> |
|||
Access the integer variable through the pointer: |
Access the integer variable through the pointer: |
||
<lang c>int v = *pointer; /* sets v to the value of var (i.e. 3) */ |
|||
*pointer = 42; /* sets var to 42 */</lang> |
|||
Change the pointer to refer to another object |
Change the pointer to refer to another object |
||
<lang c>int othervar; |
|||
pointer = &othervar;</lang> |
|||
Change the pointer to not point to any object |
Change the pointer to not point to any object |
||
<lang c>pointer = NULL; /* needs having stddef.h included */</lang> |
|||
or |
or |
||
<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 |
||
<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: |
||
<lang c>int array[10]; |
|||
pointer = array; |
|||
/* or alternatively: */ |
|||
pointer = &array[0];</lang> |
|||
Move the pointer to another object in the array |
Move the pointer to another object in the array |
||
<lang c>pointer += 3; /* pointer now points to array[3] */ |
|||
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 |
||
<lang c>v = pointer[3]; /* accesses third-next object, i.e. array[4] */ |
|||
v = pointer[-1]; /* accesses previous object, i.e. array[0] */ |
|||
/* or alternatively */ |
|||
v = *(pointer + 3); /* array[4] */ |
|||
v = *(pointer - 1); /* array[0] */</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|c sharp|C#}}== |
=={{header|c sharp|C#}}== |
||
Line 201: | Line 238: | ||
Val After : 1 |
Val After : 1 |
||
</pre> |
</pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{omit from|Clojure}} |
{{omit from|Clojure}} |
||
Line 269: | Line 278: | ||
Variable Declaration |
Variable Declaration |
||
<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 |
||
<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 |
||
<lang delphi>MyRecord = Record |
|||
FName : string[20]; |
|||
LName : string[20]; |
|||
end;</lang> |
|||
Variable Declaration |
Variable Declaration |
||
<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 } |
|||
tFoo = record |
|||
value1, value2: integer; |
|||
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: |
||
<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. |
||
<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: |
||
<lang delphi>Inc(PtrVar); //increment by one element |
|||
Inc(PtrVar, 4); //incremement by four elements |
|||
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: |
||
<lang delphi>{$POINTERMATH ON} |
|||
PrevIntVar := MyIntPtr[-1]; |
|||
Rec4 := MyRecPtr[4];</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |