| Topics: | Language and Phobos -DGui (example placeholder - can be any library) | ||
| Previous | Lessons: | 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -T | Next |
| Subpages: | What are Pointers? -What are Arrays? |
In this chapter, you will learn about how to use pointers and static arrays in D. You will also learn how to pass by reference without using pointers. The lesson text will not explain the concepts behind pointers and arrays, and it will assume that you have that knowledge from C or C++. D also has dynamic arrays and array slices, both of which will be covered in a later lesson.
importstd.stdio;voidadd_numbers(int*a,int*b){*a=*a+*b;}voidmain(){inta=50;int*b=&a;*b=30;writeln(a);// 30// int* error = &50; Error: constant 50 is not an lvalueintc=2;intd=2;add_numbers(&c,&d);writeln(c);// 4}
If you're a C++ programmer and you read the above thinking it's a step back from C++ references, then rejoice: while D supports pointers, it also supports better solutions for passing variables by reference to functions:
importstd.stdio;voidadd_numbers(refinta,intb){a+=b;}voidmain(){intc=2;intd=2;add_numbers(c,d);writeln(c);// 4// add_numbers(4, 2); Error: integer literals are not lvalues and cannot be passed by reference}
There is also special support forout parameters:
importstd.stdio;voidinitializeNumber(outintn){n=42;}voidmain(){inta;initializeNumber(a);writeln(a);// 42}
Usingout,n in initializeNumber is default initialized at the beginning of the function.
importstd.stdio;voidmain(){int[5]a;a[0]=1;writeln(a);// [1, 0, 0, 0, 0]// a[10] = 3; Error: Array index 10 is out of boundsint*ptr_a=&a[0];ptr_a[0]=5;ptr_a[4]=3;writeln(a);// [5, 0, 0, 0, 3]// ptr_a[10] = 3; // Bad: This memory is not owned by 'a' as it is out of bounds// Compiler would allow it, but the result is undefined!int[3]b=3;writeln(b);// [3, 3, 3]b[]=2;writeln(b);// [2, 2, 2]}
The syntax in D is the same as C and C++. The operator& takes the reference of an object, and* dereferences.
In D, a pointer to typeA is:
typeA*
and the code for declaring one is:
typeA*identifier;
.
Static arrays have fixed lengths. You declare them like this:
byte[10]array_of_ten_bytes;
You can also do this:
byte[2]array_of_two_bytes=[39,0x3A];
Arrays are indexed starting with the number zero for the first element. The compiler will catch you if you attempt to access an element with an index greater than or equal to that array's length. You access elements of an array like this:
my_array[index]
You can also fill an array with a single value, so that all of that array's elements equal that value:
int[7]a=2;writeln(a);// [2, 2, 2, 2, 2, 2, 2]a[]=7;writeln(a);// [7, 7, 7, 7, 7, 7, 7]
Look at this code:
importstd.stdio;voidaddOne(int[3]arr){arr[0]+=1;// this means arr[0] = arr[0] + 1}voidmain(){int[3]array1=[1,2,3];addOne(array1);writeln(array1);// [1, 2, 3]}
The output is[1,2,3], not[2,2,3]. Why? It's because static arrays are copied whenever they are passed as arguments to a function. Thearr in theaddOne function is only a copy ofarray1. Rewrite it like this:
importstd.stdio;voidaddOne(refint[3]arr){arr[0]+=1;}voidmain(){int[3]array1=[1,2,3];addOne(array1);writeln(array1);// [2, 2, 3]}