Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:48:19+00:00 2026-06-15T16:48:19+00:00

This piece of code conceptually does the same thing for the three pointers (safe

  • 0

This piece of code conceptually does the same thing for the three pointers (safe pointer initialization):

int* p1 = nullptr;
int* p2 = NULL;
int* p3 = 0;

And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:48:20+00:00Added an answer on June 15, 2026 at 4:48 pm

    In that code, there doesn’t seem to be an advantage. But consider the following overloaded functions:

    void f(char const *ptr);
    void f(int v);
    
    f(NULL);  //which function will be called?
    

    Which function will be called? Of course, the intention here is to call f(char const *), but in reality f(int) will be called! That is a big problem1, isn’t it?

    So, the solution to such problems is to use nullptr:

    f(nullptr); //first function is called
    

    Of course, that is not the only advantage of nullptr. Here is another:

    template<typename T, T *ptr>
    struct something{};                     //primary template
    
    template<>
    struct something<nullptr_t, nullptr>{};  //partial specialization for nullptr
    

    Since in template, the type of nullptr is deduced as nullptr_t, so you can write this:

    template<typename T>
    void f(T *ptr);   //function to handle non-nullptr argument
    
    void f(nullptr_t); //an overload to handle nullptr argument!!!
    

    1. In C++, NULL is defined as #define NULL 0, so it is basically int, that is why f(int) is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take this piece of code: int a; int *pointer = &a; int **b =
This piece of code used to work in MVC 1 but it does not
This piece of code (in Scala) val contents = { assert(mainWindow.detailsPane != null) new
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
I found this piece of code on Wikipedia. #include <stdio.h> int main(void) { int
Why this piece of code compiles? #include <iostream> int foo(int x) { if(x ==
Why this piece of code does not work? How to set the length of
Consider this piece of code: Uint counter = 0; int* p1; int* p2; deque<int>
executing this piece of code: SimpleDateFormat sdfIn = new SimpleDateFormat(yyyy-MM-dd); Date date = null;
Running this piece of code will print null public class Weird { static class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.