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 6242703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:00:19+00:00 2026-05-24T12:00:19+00:00

In C, NULL is defined as (void *)0 whereas in C++ it is 0

  • 0

In C, NULL is defined as (void *)0 whereas in C++ it is 0. Why is it so?
In C I can understand that if NULL is not typecast to (void *) then compilers may/may not generate warning. Other than this, is there any reason?

  • 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-05-24T12:00:21+00:00Added an answer on May 24, 2026 at 12:00 pm

    Back in C++03, a null pointer was defined by the ISO specification (§4.10/1) as

    A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.

    This is why in C++ you can write

    int* ptr = 0;
    

    In C, this rule is similar, but is a bit different (§6.3.2.3/3):

    An integer constant expression with the value 0, or such an expression cast to type
    void *, is called a null pointer constant.55) If a null pointer constant is converted to a
    pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal
    to a pointer to any object or function.

    Consequently, both

    int* ptr = 0;
    

    and

    int* ptr = (void *)0
    

    are legal. However, my guess is that the void* cast is here so that statements like

    int x = NULL;
    

    produce a compiler warning on most systems. In C++, this wouldn’t be legal because you can’t implicitly convert a void* to another pointer type implicitly without a cast. For example, this is illegal:

    int* ptr = (void*)0; // Legal C, illegal C++
    

    However, this leads to issues because the code

    int x = NULL;
    

    is legal C++. Because of this and the ensuing confusion (and another case, shown later), since C++11, there is a keyword nullptr representing a null pointer:

    int* ptr = nullptr;
    

    This doesn’t have any of the above problems.

    The other advantage of nullptr over 0 is that it plays better with the C++ type system. For example, suppose I have these two functions:

    void DoSomething(int x);
    void DoSomething(char* x);
    

    If I call

    DoSomething(NULL);
    

    It’s equivalent to

    DoSomething(0);
    

    which calls DoSomething(int) instead of the expected DoSomething(char*). However, with nullptr, I could write

    DoSomething(nullptr);
    

    And it will call the DoSomething(char*) function as expected.

    Similarly, suppose that I have a vector<Object*> and want to set each element to be a null pointer. Using the std::fill algorithm, I might try writing

    std::fill(v.begin(), v.end(), NULL);
    

    However, this doesn’t compile, because the template system treats NULL as an int and not a pointer. To fix this, I would have to write

    std::fill(v.begin(), v.end(), (Object*)NULL);
    

    This is ugly and somewhat defeats the purpose of the template system. To fix this, I can use nullptr:

    std::fill(v.begin(), v.end(), nullptr);
    

    And since nullptr is known to have a type corresponding to a null pointer (specifically, std::nullptr_t), this will compile correctly.

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

Sidebar

Related Questions

I have a table defined by: CREATE TABLE bar_table ( _id INTEGER NOT NULL
Can Thread.getContextClassLoader() be null ? The javadoc is not really clear. Should a library
Can a C++ user-defined literal operator ever be passed a null pointer? This is
I have a @variabletable simply defined as EOMDate(datetime), DandA(float), Coupon(float), EarnedIncome(float) 04/30/2008, 20187.5,17812.5,NULL 05/31/2008,
it's all in index.php /* Define site root */ defined('DOCUMENT_ROOT') ? null : define('DOCUMENT_ROOT',realpath(dirname(__FILE__)));
Null or empty string -- is one better than the other to represent no
Dupe: Null Difference A lifetime ago I came across an article that explained that
I defined a dialog the following way: public void displayAvailableDevices(Vector<UserDevice> availableDevices) { connector.setDevicesFound(true); UserDevice[]
I have a joinable pthread runner function defined as below: void *sumOfProducts(void *param) {
Assume someClass is a class defined in C# with some method int doSomething(void) ,

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.