设为首页 加入收藏

TOP

5.12.3 Other Implicit Conversions (1)
2013-10-07 15:24:55 来源: 作者: 【 】 浏览:60
Tags:5.12.3 Other Implicit Conversions

Pointer Conversions

In most cases when we use an array, the array is automatically converted to a pointer to the first element:

  1. int ia[10]; // array of 10 ints  
  2. int* ip = ia; // convert ia to pointer to first element 

The exceptions when an array is not converted to a pointer are: as the operand of the address-of (&) operator or of sizeof, or when using the array to initialize a reference to the array. We’ll see how to define a reference (or pointer) to an array
in Section 7.2.4 (p. 240).

There are two other pointer conversions: A pointer to any data type can be converted to a void*, and a constant integral value of 0 can be converted to any pointer type.

因此 char* p = false; 是合法的语句,而 char* p =true; 是非法的,因为 false提升为int 之后的值是0。

Conversions to bool

Arithmetic and pointer values can be converted to bool. If the pointer or arithmetic value is zero, then the bool is false; any other value converts to true:

  1. if (cp) /* ... */ // true if cp is not zero  
  2. while (*cp) /* ... */ // dereference cp and convert resulting char to bool 

Here, the if converts any nonzero value of cp to true. The while dereferences cp, which yields a char. The null character has value zero and converts to false. All other char values convert to true.

Arithmetic Type and bool Conversions

Arithmetic objects can be converted to bool and bool objects can be converted to int. When an arithmetic type is converted to bool, zero converts as false and any other value converts as true. When a bool is converted to an arithmetic type, true becomes one and false becomes zero:

  1. bool b = true;  
  2. int ival = b; // ival == 1  
  3. double pi = 3.14;  
  4. bool b2 = pi; // b2 is true  
  5. pi = false// pi == 0 

Conversions and Enumeration Types

Objects of an enumeration type (Section 2.7, p. 62) or an enumerator can be automatically converted to an integral type. As a result, they can be used where an integral value is required—for example, in an arithmetic expression:

  1. // point2d is 2, point2w is 3, point3d is 3, point3w is 4  
  2. enum Points { point2d = 2, point2w,  
  3. point3d = 3, point3w };  
  4. const size_t array_size = 1024;  
  5. // ok: pt2w promoted to int  
  6. int chunk_size = array_size * pt2w;  
  7. int array_3d = array_size * point3d; 

The type to which an enum object or enumerator is promoted is machine-defined and depends on the value of the largest enumerator. Regardless of that value, an enum or enumerator is always promoted at least to int. If the largest enumerator does not fit in an int, then the promotion is to the smallest type larger than int (unsigned int, long or unsigned long) that can hold the enumerator value.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.12.3 Other Implicit Conversio.. 下一篇5.10.3 Order of eva luation (3)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)