...how when some functions return 0, it was a success, yet when others return NULL, it's a failure?
Printable View
...how when some functions return 0, it was a success, yet when others return NULL, it's a failure?
err, no?
Depends on exactly the functions you are talking about here. But null and 0 are two very distinct things. And even when used in a return value to denote failure, they still have a distinct semantics that is quite intentional.
Care to give an example of what functions you are talking about. Are we discussing C here, per chance?
Yeah, C. strcmp() and strncmp() return 0 if two strings are equal (success). And functions like malloc() and fopen() return NULL if there's not enough memory, or if the file couldn't be opened (failure). Last time I checked, NULL == 0. I thought that was pretty weird.
I'll give you a few minutes to think why's that...
Think of what the function is returning exactly and what is the use you make of what is returned.
Never mind.
:P
That's because strcmp() doesn't check to see whether the two strings match, it compares their lexical value. Considering you probably don't know what that means, you really should read the manual on strcmp().
As far as malloc() goes... what else could it possibly return on failure?
Also, I would disagree about NULL != 0. In C(++) they are quite the same thing, often times even semantically. Calling zero, null, only implies that the variable holding the value in question, is probably a pointer.
EDIT: Never mind.. :D
*facepalm*Quote:
Originally Posted by CommonTater
You should take your own advice...
C: What is NULL and how is it defined?
C++: Should I use NULL or 0?
>> Also, I would disagree about NULL != 0. In C(++) they are quite the same thing, often times even semantically.[/quote]
So, you are saying it's quite alright to write:
if (myClass == 0) do_something();
Maybe the word "semantics" has a different meaning to you and me. Coincidentally, this is an old, old, old, debate that I have no wish of starting with you. So if your answer to the above code is "that is perfectly alright", suit yourself.
>> Calling zero, null, only implies that the variable holding the value in question, is probably a pointer.
Ah!... I love the smell of uncertainty in the strongly-typed morning.
Maybe, maybe not. It depends on what is myClass. If it is a class name, then that would result in a syntax error, so the answer to your question becomes blatantly obvious.Quote:
Originally Posted by Mario F.
nullptr FTW!Quote:
Originally Posted by Mario F.
It's not that either. It is the value 0, cast to a void*. It doesn't point to 0; it doesn't point anywhere (that is, after all, the point of NULL).
(Edit to add: If you want to say that, in the case of a pointer, "having the value of" and "pointing to" are the same thing (i.e.
where x has the value 0x100 and points to 0x100), that would be fine; however, that's not where we started this sidetrack....)Code:void *x = 0x100;