D 编程 - sizeof 运算符


还有一些其他重要的运算符,包括sizeof? :由D语言支持。

操作员 描述 例子
大小() 返回变量的大小。 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