D-programming-sizeof-operator
提供:Dev Guides
Dプログラミング-sizeof演算子
*sizeof* や*?など、他の重要な演算子はほとんどありません。 :* D言語でサポートされています。
Operator | Description | Example |
---|---|---|
sizeof() | Returns the size of an variable. | sizeof(a), where a is integer, returns 4. |
& | Returns the address of a variable. | &a; gives actual address of the variable. |
* | Pointer to a variable. | *a; gives pointer to a variable. |
? : | Conditional Expression | If condition is true then value X: Otherwise value Y. |
例
Dプログラミング言語で使用可能なすべての雑多な演算子を理解するには、次の例を試してください-
import std.stdio;
int main(string[] args) {
int a = 4;
short b;
double c;
int* ptr;
/*example of sizeof operator*/
writefln("Line 1 - Size of variable a = %d\n", a.sizeof );
writefln("Line 2 - Size of variable b = %d\n", b.sizeof );
writefln("Line 3 - Size of variable c= %d\n", c.sizeof );
/*example of & and* operators */
ptr = &a;/* 'ptr' now contains the address of 'a'*/
writefln("value of a is %d\n", a);
writefln("*ptr is %d.\n", *ptr);
/*example of ternary operator*/
a = 10;
b = (a == 1) ? 20: 30;
writefln( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
writefln( "Value of b is %d\n", b );
return 0;
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20