Anyone else think it's kinda weird...

This is a discussion on Anyone else think it's kinda weird... within the General Discussions forums, part of the Community Boards category; ...how when some functions return 0, it was a success, yet when others return NULL, it's a failure?...

Anyone else think it's kinda weird...

Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

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?

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.

Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

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.

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.

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 get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

>> 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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.