2

Is there a difference in C++11 between the following pointer initialization:

#include <pwd.h>
int main(int argc, char const *argv[])
{
 passwd *p1{};
 passwd *p2{nullptr};
 return 0;
}

If I understand correctly, p1 is zero-initialized, and p2 is direct-initialized from nullptr. Is it the same thing? Is it possible to use initialization, as in the case of p1, because it requires fewer keystrokes?

asked May 5, 2022 at 17:48
2
  • 5
    They're equivalent, and which to use is entirely opinion-based. You left out the most common initialization vernacular: passwd *p3 = nullptr;, which is arguably the most clear in function and intent, regardless of how many precious keystrokes, and the extre half-second it takes to tap them, you're saving. Commented May 5, 2022 at 17:54
  • 2
    "p1 is zero-initialized" - technically, it is value-initialized, but since p1 is a pointer type then the resulting value is zero-initialized. Commented May 5, 2022 at 18:04

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.