Writing an upgrade to my program I'd like to start using the nullptr, a cleaner replacement to NULL. My question is can I use it everywhere, for example in checking the strstr return value?

if (strstr(szLong,"tag") == nullptr) 

Presumably will work as well as

if (strstr(szLong,"tag") == NULL) 

?

Recommended Answers

All 13 Replies

Yes, you can use nullptr everywhere as a replacement for NULL. Even in the places where NULL was actually a degenerate case and didn't work as expected...unless you want the degenerate behavior, of course. ;)

Thanks. Is there some "official" place where that is said, I couldn't find one.

The IS is the place where something about C++ is "officially" said.

2.14.7 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.

Oitside of that, this is about as close as one can get:
http://www.stroustrup.com/C++11FAQ.html#nullptr

Thanks. Is there some "official" place where that is said, I couldn't find one.

Well, there's only one "official" place and that's the ISO standard. Now that I think of it I don't recall seeing it explicitly written out anywhere else authoritative (if you don't count my reading of the standard to be authoritative ;D). However, it's been stated explicitly in a number of conferences, presentations, and talks about C++0x/C++11 that I've attended where the presenters were most certainly authoritative (Herb Sutter, Scott Meyers, Andrei Alexandrescu, Bjarne Stroustrup, Stephan Lavavej, etc...).

Never new of nullptr.
Just done a quick replace ina large code with nullptr for NULL, and it turns out I must have been using NULL incorrectly in a lot of places where int 0 should have been used.

Something new everyday.

it turns out I must have been using NULL incorrectly in a lot of places where int 0 should have been used.

Yup, it's a subtle trap. I've been advocating only using NULL in pointer context for years. I guess now the folks who ignored me will realize why it was important. ;)

NULL is defined in <stdio.h> as (void*)0. This can cause problems with some C++ code. With current C/C++ compilers, you are safe to use a simple 0 for null values. IE:

void* vptr = 0; // ok
int* iptr = 0; // ok
double* dptr = NULL; // not ok, depending upon compiler

NULL is defined in <stdio.h> as (void*)0.

In C that's a possible definition. In C++ it's defined simply as 0 (or something equivalent), otherwise you'd get an error every time you used NULL because there's no implicit conversion from void*. NULL would be completely useless in that case, so C++'s NULL fits the C++ version of a null pointer constant rather than the C version. If your compiler gives you an error for double* dptr = NULL;, it's a non-conforming compiler.

Ok, thanks for all this explication, I'm learning by the minute.

But since I'm an old awkward old codger... in Windows MFC code there is often a NULL pointer defualt parameter, whenc constructing dialogs for example. If I change that to nullptr (in my dialogs for example) there should be no bad effects.

Here is an example declaration:

CDelimsDlg(const CString& csDataName,
           const char* const pszBuffer, 
           const CFormat& Format, 
           const CString& Ext, // ".TXT" or ".PTS" etc
           CWnd* pParent= NULL);   

I can change that NULL to nullptr I suppose?

I cannot answer that with any certainty sorry.

But also notice nullptr also not work for what NULL can with something like.

char * nullterm = "null terminated\0";
    int i = 0;
    while (nullterm[i] != nullptr) {// Error: operands not compatible (char and std::nullptr_t)
        cout << nullterm[i];
        i++;
    }
    cout << "\n";

I can change that NULL to nullptr I suppose?

Yes.

But also notice nullptr also not work for what NULL can with something like.

In other words, nullptr won't work outside of a pointer context, which is exactly what you want. NULL was never intended to be used as an equivalent to '\0' outside of pointer context, that came about from people who misunderstood the word "null" and also made assumptions about the type of NULL.

Years ago I picked up the habit, I don't know where from, of always using

#define NUL char(0)

for 0 terminators in ASCII strings, and NULL for pointers.

Years ago I picked up the habit, I don't know where from, of always using

#define NUL char(0)

for 0 terminators in ASCII strings, and NULL for pointers.

I wouldn't recommend that at all, and to be honest I don't see the point of using macros for such things anymore in C++. If you want an int to be zero, use 0. If you want a char to be a terminator, use '\0'. If you want a null pointer, use nullptr (or use NULL if you're using C or have to compile for something older than C++11). To me, it only makes sense to write exactly what you mean when writing code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.