D語言sizeof運算子


D語言的支援還有其他一些重要的運算子,包括的sizeof和? :。

運算子 描述 範例
sizeof 返回一個變數的大小。 a.sizeof, 其中a是整數,將返回4。
& 返回一個變數的地址。 &a; 將得到的變數actaul地址。
* 指向變數的指標。 *a; 指標變數。
? : 條件表示式 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
", a.sizeof );
   writefln("Line 2 - Size of variable b = %d
", b.sizeof );
   writefln("Line 3 - Size of variable c= %d
", c.sizeof );

   /* example of & and * operators */
   ptr = &a;	/* 'ptr' now contains the address of 'a'*/
   writefln("value of a is  %d
", a);
   writefln("*ptr is %d.
", *ptr);

   /* example of ternary operator */
   a = 10;
   b = (a == 1) ? 20: 30;
   writefln( "Value of b is %d
", b );

   b = (a == 10) ? 20: 30;
   writefln( "Value of b is %d
", b );
   return 0;
}

當編譯並執行上面的程式它會產生以下結果:

value of a is  4
*ptr is 4.
Value of b is 30
Value of b is 20