I want to ask what does the (0)
mean after the pointer i.e. Node* ptr1(0)
.
struct Node
{
string info;
Node * next
};
int main()
{
Node* ptr1 (0), *ptr2 (0),
ptr1 = new Node;
ptr2 = new Node;
}
I want to ask what does the (0)
mean after the pointer i.e. Node* ptr1(0)
.
struct Node
{
string info;
Node * next
};
int main()
{
Node* ptr1 (0), *ptr2 (0),
ptr1 = new Node;
ptr2 = new Node;
}
It means initializing the pointer with 0 or null.
Refer to this question for understanding the explanation behind it (they're actually the same; 0
and null
in this context)
For any integral type T
, the following two declarations are effectively equivalent:
T obj(0);
T obj = 0;
And since 0
is a null pointer constant, all you're doing here is initialising your two pointers to be null.
There are lots of ways to initialise objects, but consider how you declare objects of class type:
MyClass obj(someArguments...);
It's the same thing.
This is direct initialization.
if T is a non-class type, standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version of T.
For pointer type, initialize it with 0 makes it a null pointer. See Pointer conversions.
A null pointer constant (see NULL), can be converted to any pointer type, and the result is the null pointer value of that type.
This is a constructor call. Since there is no constructor defined the compiler supplies one. After C++11 the preferred form is 'Node * ptr1 {0};' using braces.