Search code examples
c++visual-c++pointersinitializationbuilt-in-types

How is "int* ptr = int()" value initialization not illegal?


The following code (taken from here):

int* ptr = int();

compiles in Visual C++ and value-initializes the pointer.

How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer.

How is the code above not illegal?


Solution

  • int() is a constant expression with a value of 0, so it's a valid way of producing a null pointer constant. Ultimately, it's just a slightly different way of saying int *ptr = NULL;