TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

 Home » Tip Bank » C++ » Pointer Arithmetic
Language: C++
Sep 25, 2000

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Enumerator with Zero Value and Null Pointer Constants

Here's a topic that was recently discussed at the Standardization committee. Suppose we have the following enum type:

``````
``````

Is it legal to use the enumerator good, whose value is zero, as a null pointer constant? Put differently, is the following declaration allowed?

``````
void *p = good;
``````

After all, a null pointer constant in C++ is either 0 or 0L. The answer is "no". This declaration isn't allowed because the standard says that "a null pointer constant is an integral constant expression rvalue of integer type that evaluates to zero." Let's analyze it. A null pointer constant, e.g., NULL, 0, or 0L, must be an integral constant expression. Indeed, an enumerator is an integral constant expression. However, to qualify as a null constant expression, an identifier must also be an "rvalue of integer type". Enumerators are rvalues but they are not of integer type. Therefore, the declaration

``````
void *p = good;
``````

is illegal. There is another way to reach the same conclusion without resorting to C++ casuistry: C++ allows only one implicit conversion per expression. Therefore, had we used the literal zero, as in:

``````
void *p = 0;
``````

The compiler would have implicitly converted the literal zero to type void * and assigned it to p. However, once we use good as an initializer, two implicit conversions are necessary. The first converts the enumerator to the integer zero, and the second converts the integer zero to void *. However, because C++ allows only one implicit conversion, the following declaration is invalid:

``````
void *p=good; // requires two implicit conversions; invalid
``````

Danny Kalev

 Submit a Tip Browse "C++" Tips Browse All Tips
Thanks for your registration, follow us on our social networks to keep up-to-date