Pointers and references: Difference between revisions

Added basic pointer/reference examples for D language
(Added basic pointer/reference examples for D language)
Line 162:
Accessing another object of the array through the reference:
v = (&ref)[3]; // read value of array[3]; however doing this is bad style
 
=={{header|D}}==
 
Grabbing variable address and placing it in a pointer
<lang d>
int var;
int* ptr = &var;
</lang>
 
Depending on variable type, D will automatically pass either by value or reference
by value: structs, statically sized arrays, and other primitives (int, char, etc...)
by reference: classes, dynamically sized arrays, array slices
<lang d>
struct S{}
class C{}
 
void foo(S s){} // pass by value
void foo(C c){} // pass by reference
void foo(int i){} // pass by value
void foo(int[4] i){} // pass by value
void foo(int[] i){} // pass by reference
void foo(ref T t){} // pass by reference regardless of what type T really is
</lang>
 
=={{header|Delphi}}==
Anonymous user