Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

D (The Programming Language)/d2/Pointers, Pass-By-Reference and Static Arrays

25% developed
From Wikibooks, open books for an open world
<D (The Programming Language)
(Redirected fromD (The Programming Language)/d2/Lesson 7)
Topics:Language and Phobos -DGui (example placeholder - can be any library)
PreviousLessons:0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -TNext
Subpages:What are Pointers? -What are Arrays?

Lesson 7: Pointers, Pass-By-Reference and Static Arrays

[edit |edit source]

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.

Introductory Code

[edit |edit source]

Using Pointers

[edit |edit source]
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}

Pass By Reference

[edit |edit source]

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.

Using Static Arrays

[edit |edit source]
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]}

Concepts

[edit |edit source]

Reference and Dereference

[edit |edit source]

The syntax in D is the same as C and C++. The operator& takes the reference of an object, and* dereferences.

Pointer Types

[edit |edit source]

In D, a pointer to typeA is:

typeA*

and the code for declaring one is:

typeA*identifier;

.

Static Arrays

[edit |edit source]

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]

As Function Arguments

[edit |edit source]

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]}
Retrieved from "https://en.wikibooks.org/w/index.php?title=D_(The_Programming_Language)/d2/Pointers,_Pass-By-Reference_and_Static_Arrays&oldid=3676134"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp