D 程式設計 - sizeof 運算子



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

運算子 描述 示例
sizeof() 返回變數的大小。 sizeof(a),其中 a 為整數,返回 4。
& 返回變數的地址。 &a; 給出變數的實際地址。
* 指向變數的指標。 *a; 給出指向變數的指標。
? : 條件表示式 如果條件為真,則返回值 X;否則返回值 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
d_programming_operators.htm
廣告