D (程式語言)/d2/指標、按引用傳遞和靜態陣列
外觀
< D (程式語言)
在本章中,您將學習如何在 D 中使用指標和靜態陣列。您還將學習如何在不使用指標的情況下按引用傳遞。課文不會解釋指標和陣列背後的概念,它將假設您已經從 C 或 C++ 中瞭解了這些知識。D 還有動態陣列和陣列切片,這將在後面的課程中介紹。
import std.stdio;
void add_numbers(int* a, int* b)
{
*a = *a + *b;
}
void main()
{
int a = 50;
int* b = &a;
*b = 30;
writeln(a); // 30
// int* error = &50; Error: constant 50 is not an lvalue
int c = 2;
int d = 2;
add_numbers(&c, &d);
writeln(c); // 4
}
如果您是 C++ 程式設計師,並且您在閱讀上面的內容時認為它與 C++ 引用相比有所倒退,那麼請欣喜:雖然 D 支援指標,但它也支援更好的解決方案,用於將變數按引用傳遞給函式。
import std.stdio;
void add_numbers(ref int a, int b)
{
a += b;
}
void main()
{
int c = 2;
int d = 2;
add_numbers(c, d);
writeln(c); // 4
// add_numbers(4, 2); Error: integer literals are not lvalues and cannot be passed by reference
}
它還特別支援 *輸出引數*。
import std.stdio;
void initializeNumber(out int n)
{
n = 42;
}
void main()
{
int a;
initializeNumber(a);
writeln(a); // 42
}
使用 `out`,`n` 在 `initializeNumber` 中在函式開始時被預設初始化。
import std.stdio;
void main()
{
int[5] a;
a[0] = 1;
writeln(a); // [1, 0, 0, 0, 0]
// a[10] = 3; Error: Array index 10 is out of bounds
int* 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]
}
D 中的語法與 C 和 C++ 相同。運算子 `&` 獲取物件的引用,而 `*` 解引用。
在 D 中,指向 typeA 的指標為
typeA*
宣告一個的程式碼是
typeA* identifier;
.
靜態陣列具有固定長度。您可以像這樣宣告它們:
byte[10] array_of_ten_bytes;
您也可以這樣做:
byte[2] array_of_two_bytes = [39, 0x3A];
陣列從數字零開始索引,第一個元素的索引為零。如果嘗試訪問索引大於或等於該陣列長度的元素,編譯器會捕獲它。您可以像這樣訪問陣列的元素:
my_array[index]
您還可以用單個值填充陣列,使該陣列的所有元素都等於該值:
int[7] a = 2;
writeln(a); // [2, 2, 2, 2, 2, 2, 2]
a[] = 7;
writeln(a); // [7, 7, 7, 7, 7, 7, 7]
看看這段程式碼:
import std.stdio;
void addOne(int[3] arr)
{
arr[0] += 1;
// this means arr[0] = arr[0] + 1
}
void main()
{
int[3] array1 = [1, 2, 3];
addOne(array1);
writeln(array1); // [1, 2, 3]
}
輸出為 `[1,2,3]`,而不是 `[2,2,3]`。為什麼?因為靜態陣列在作為引數傳遞給函式時會被複制。`addOne` 函式中的 `arr` 只是 `array1` 的副本。像這樣重寫它:
import std.stdio;
void addOne(ref int[3] arr)
{
arr[0] += 1;
}
void main()
{
int[3] array1 = [1, 2, 3];
addOne(array1);
writeln(array1); // [2, 2, 3]
}