4

I found strange from my point of view compiler behavior, It allows assign Boolean value to * char.

char * k= false;

Why? But after assignment * char is still not initialized. Why compilers doesn't allows assign int value?

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
vico
  • 17,051
  • 45
  • 159
  • 315
  • That makes `k` a NULL pointer just like `char * k = 0`. – Alexey Frunze Oct 24 '12 at 08:18
  • It is an implicit conversion from `bool` to `char *`. However, this works only for constant expressions that convert to (int) 0, which is a special initializer for pointers. Would be interesting if someone quoted the respective rule from the standard. – peterchen Oct 24 '12 at 08:37

4 Answers4

5

It will be implicitly converting the boolean value false to an integer with value zero and as such declaring a NULL pointer. It is effectively no different from

char* k = 0;

which is valid syntax

peterchen
  • 40,917
  • 20
  • 104
  • 186
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
2

C++03 Standard, #4.10:

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.

5.19:

An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type tem- plate parameters of integral or enumeration types, and sizeof expressions.

false is a boolean literal, therefore it falls into the category of a constant expression, so it can qualify as a null pointer constant.

Andriy
  • 8,486
  • 3
  • 27
  • 51
0

false and true are shortcuts for 0 and 1. For pointer you use NULL which define NULL 0. So it correct syntax.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0

The compiler allows it because in C++ false is the same as 0 and NULL.

Personally, at least for assignments, I find it easier to understand and more correct to use NULL to indicate a null pointer.

Btw, before C++, on some systems NULL was actually a macro defined as (void*)0xffff; some background on that can be found in this answer.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I would need to verify that in the standard, but in C++, NULL is 0. Looking at B. Stroustrup page, I have the confirmation from C++'s initial creator itself: http://www.stroustrup.com/bs_faq2.html#null – paercebal Oct 24 '12 at 08:39
  • @paercebal I was furiously looking to find where I remembered it from, so I've rephrased my answer in the meantime :) – Ja͢ck Oct 24 '12 at 08:45
  • FWIW, [Wikipedia says](http://en.wikipedia.org/wiki/Boolean_datatype) "C++ has a separate Boolean data type ('bool'), but with automatic conversions from scalar and pointer values that are very similar to those of C". Don't have the standard at hand right now. – jweyrich Oct 24 '12 at 08:48
  • A precision: From section 4.10 of the standard (N3337), we have: `A null pointer constant is an integral constant expression (5.19) prvalue **of integer type that evaluates to zero** or a prvalue of type std::nullptr_t`. This means that no matter the system, in C++, NULL will always be an integer type that will evaluate to zero. – paercebal Oct 24 '12 at 08:59
  • @paercebal I must admit that I've never read the C++ standard, but it's good that this was standardized :) I found my source btw, I've added a link to another answer. – Ja͢ck Oct 24 '12 at 09:27
  • @Jack : You give me too much credit. I knew about the integer zero thing from Stroustrup's web page. But I didn't read the C++ standard (but I still plan to...). Just made a search for NULL in the N3337 document (google for it), and read each occurrence until I found what I needed... ^_^ ... – paercebal Oct 24 '12 at 21:16