2

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; } 
1
  • 4
    It should be noted that instead of initialising pointers with 0 or NULL one should use nullptr instead as of c++ 11. Commented May 20, 2016 at 15:05

5 Answers 5

3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

0 and NULL are not the same.
Also mind that that post has 7 years. You can also use nullptr
Well of course they're not, but in this context @LightnessRacesInOrbit they represent the same...
1

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.

Comments

1

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.

Comments

1

This simply initializes the pointer. And it is initializing it to be null.

Comments

0

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.

2 Comments

There is no constructor for pointers, compiler-supplied or otherwise.
Thanks, 'Node * ptr1 {nullptr};' would be a better also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.