Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ nil vs NULL

Tags:

c++

null

OK, I have some C++ code in a header that is declared like this:

void StreamOut(FxStream *stream,const FxChar *name = nil);

and I get: error:

'nil' was not declared in this scope

nil is a pascal thing, correct?

Should I be using NULL?

I thought they were both the same or at least Zero, no?

like image 322
JT. Avatar asked Nov 05 '09 21:11

JT.


People also ask

Is nil same as?

Nil means the same as zero. It is usually used to say what the score is in sports such as rugby or football.

Is nil the same as null Ruby?

Let's start out with “Nil” since it's the most common and easy-to-understand way of representing nothingness in Ruby. In terms of what it means, Nil is exactly the same thing as null in other languages.

Is same as nil Swift?

Swift's nil is not the same as nil in Objective-C. In Objective-C, nil is a pointer to a non-existent object. In Swift, nil is not a pointer—it is the absence of a value of a certain type. Optionals of any type can be set to nil , not just object types.


4 Answers

In C++ you need to use NULL, 0, or in some brand new compilers nullptr. The use of NULL vs. 0 can be a bit of a debate in some circles but IMHO, NULL is the more popular use over 0.

like image 138
JaredPar Avatar answered Sep 22 '22 21:09

JaredPar


nil does not exist in standard C++. Use NULL instead.

like image 43
Marcin Avatar answered Sep 22 '22 21:09

Marcin


Yes. It's NULL in C and C++, while it's nil in Objective-C.

Each language has its own identifier for no object. In C the standard library, NULL is a typedef of ((void *)0). In C++ the standard library, NULL is a typedef of 0 or 0L.

However IMHO, you should never use 0 in place of NULL, as it helps the readability of the code, just like having constant variables in your code: without using NULL, the value 0 is used for null pointers as well as base index value in loops as well as counts/sizes for empty lists, it makes it harder to know which one is which. Also, it's easier to grep for and such.

like image 27
notnoop Avatar answered Sep 26 '22 21:09

notnoop


0 is the recommended and common style for C++

like image 32
Guido Avatar answered Sep 25 '22 21:09

Guido