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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:46:06+00:00 2026-05-27T20:46:06+00:00

I found a bug in my code where I compared the pointer with ‘\0’.

  • 0

I found a bug in my code where I compared the pointer with ‘\0’.

Wondering why the compiler didn’t warn me about this bug I tried the following.

#include <cassert>

struct Foo
{
    char bar[5];
};

int main()
{
    Foo f;
    Foo* p = &f;
    p->bar[0] = '\0';
    assert(p->bar == '\0');    // #1. I forgot [] Now, comparing pointer with NULL and fails.
    assert(p->bar == 'A');     // #2. error: ISO C++ forbids comparison between pointer and integer
    assert(p->bar[0] == '\0'); // #3. What I intended, PASSES
    return 0;
}

What is special about ‘\0’ which makes #1 legal and #2 illegal?

Please add a reference or quotation to your answer.

  • 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-27T20:46:07+00:00Added an answer on May 27, 2026 at 8:46 pm

    What makes it legal and well defined is the fact that '\0' is a null pointer constant so it can be converted to any pointer type to make a null pointer value.

    ISO/IEC 14882:2011 4.10 [conv.ptr] / 1:

    A null pointer constant is an integral constant expression prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion.

    '\0' meets the requirements of “integral constant expression prvalue of integer type that evaluates to zero” because char is an integer type and \0 has the value zero.

    Other integers can only be explicitly converted to a pointer type via a reinterpret_cast and the result is only meaningful if the integer was the result of converting a valid pointer to an integer type of sufficient size.

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

Sidebar

Related Questions

Edit: This code is fine. I found a logic bug somewhere that doesn't exist
i wonder if i've found a compiler bug? i was removing some old code
Just found a bug in this code : for(String link : tempList1){ if(!tempList2.contains(link));{ listToPopulate.add(link);
I just found a bug in some code I didn't write and I'm a
We've found a bug in old code where connections aren't being closed. It's an
FindBugs has found a potential bug in my code. But it is not a
While searching a bug in my code today I found a strange thing. When
I have found a very subtle bug in my R code just now. The
I'm reviewing some legacy code and I've found a bug that causes the response
I found an unusual bug in my code recently through random ad-hoc testing. So,

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.