1

I thought the pointer pointed to NULL can be converted to 0. So I wrote the following program:

#include <iostream>
#include <regex>
#include <string>
#include <cstdlib>

bool is_strtoi(const std::string& str)
{
    char *p;
    std::strtol(str.c_str(), &p, 10);
    return *p == '\0';
}

bool is_strtoi1(const std::string& str)
{
    char *p;
    std::strtol(str.c_str(), &p, 10);
    return p; //Pointer to NULL should be converted to false;
}

int main ()
{
    std::string test1("123asd2");
    std::string test2("123123");
    std::cout << is_strtoi1(test1) << std::endl;
    std::cout << is_strtoi1(test2) << std::endl;
    std::cout << "---" << std::endl;
    std::cout << is_strtoi(test1) << std::endl; //prints 1
    std::cout << is_strtoi(test2) << std::endl;
}

Accroding to the 4.12 section of the Standard:

A zero value, null pointer value, or null member pointer value is converted to false

So why it didn't?

1
  • 2
    I don't see a single null pointer anywhere in your code. Jun 19, 2015 at 18:19

3 Answers 3

11

You're a layer of indirection out! A "null pointer" and "a pointer to null/zero" are two different things.

At its most basic:

  • A null pointer itself has the value zero.
  • It doesn't point to some other object with the value zero.

p  == 0;  // yes
*p == 0;  // no

Indeed, a char* pointing to a C-string of length 0 (i.e. {'\0'}) is not a null pointer; it is a perfectly valid pointer.

Going further, though, the notion that a pointer can "have the value zero" is actually an implementation detail. In abstract terms, pointers are not numbers and so we shall not consider a null pointer to have a numerical value such as zero. This is evident in the introduction of the nullptr keyword that cannot be implicitly converted to an integer. By removing the whole discussion about "zero", we can instead give the following improved, more appropriate example:

p  == nullptr; // yes
*p == '\0';    // no
2
  • 2
    "A null pointer itself has the value zero" = probably true, but you aren't supposed to believe that anymore. p!=nullptr is safer Jun 19, 2015 at 18:19
  • @MartinBeckett: Clarified Jun 19, 2015 at 18:20
3

This code:

bool is_strtoi1(const std::string& str)
{
    char *p;
    std::strtol(str.c_str(), &p, 10);
    return p; //Pointer to NULL should be converted to false;
}

will ONLY EVER return false if the address in p is NULL. It is guaranteed to NOT be NULL if the input string to strtol is not NULL [which isn't going to happen for std::string in any well-behaved program - and input of NULL to strtol is undefined behaviour, so it's not defined what happens to p in that case - but it's likely that you get a crash from such an attempt].

In other words, your premise for converting to bool is incorrect.

You could write it as:

 return *p;

which will return true if p points at a nul (zero) character.

2

You seem to be mixing up "null pointer" with "pointer to NULL" (whatever the latter means). The standard text you quoted does not say anything about any "pointers to NULL". The text you quoted says clearly and explicitly that null pointer is converted to false.

The code you posted does not have any null pointers, i.e. it does not have any pointers with null pointer value. Pointer p in your code points to some valid location inside the str.c_str() buffer, which means that p itself is not null. Hence there's nothing in your code that would be converted to false per the quoted passage form the standard.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.