difference between null an 0?

General FreeBASIC programming questions.
Posts: 135
Joined: Aug 29, 2008 21:13

difference between null an 0?

Postby Hard » Sep 22, 2009 20:10

hi there

is there any difference between null an 0?

thanks - hard
Site Admin
Posts: 6225
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Sep 22, 2009 20:36

In FB, numerically, I don't think so. But if I had a specific NULL value I'd have it as an Any Ptr.

Code: Select all

const NULL as any ptr = 0

Anyway, here's what WP has to say on the matter:
http://en.wikipedia.org/wiki/Pointer_%2 ... ll_pointer
Check the third paragraph there for C-specific guidance.

Strictly speaking it doesn't have to be defined as 0, but it's a nice convention to keep to.
Posts: 135
Joined: Aug 29, 2008 21:13

Postby Hard » Sep 22, 2009 21:08

so when working with pointers null is the proper way?

btw: site admin? since when? congrats!
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Sep 22, 2009 22:55

The main advantage of defining a null pointer would be if you ported to some system where some other value was used. IBM Mainframe PL/1, built in the days of 24-bit addressing, uses the value x'ff000000' for the constant NULL. It was also usable when IBM moved to 31-bit addressing. They subsequently also defined a constant SYSNULL, which is 4 bytes of zeroes.

In most systems, the value 0 is a good way of defining a null pointer; it's usually bits of the operating system which live at that address, and most apps wouldn't want to hack about with that.
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Sep 24, 2009 13:46

Aside from the other points discussed here, I like to use "null" since it describes at a glance exactly what I'm trying to express: a pointer that doesn't point to anything. "0" is very ambiguous; if I can't use an identifier, I prefer to use a literal that's at least the same type, and "cast(whatever ptr, 0)" is too much.
Posts: 8169
Joined: May 28, 2005 3:28

Postby D.J.Peters » Sep 24, 2009 17:04

#define NULL CPTR(ANY PTR,0)
#define ZERO 0
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT

Postby notthecheatr » Sep 24, 2009 19:56

Though they are generally stored the same way (to a computer, NULL == 0), it's good to keep the distinction in mind.

NULL literally means nothing; whereas 0 signifies the number 0. NULL may have different equivalents in different types; it means no number at all (rather than 0), or no characters at all in a string (the empty string ""). This actually has significance in an untyped language. Pointers are actually stored as numbers just like everything else, but it's good to think of them not as numbers but as a pointer to something; so NULL has the important distinction of, rather than merely being 0, being the pointer that points to nothing.
Posts: 1156
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Sep 24, 2009 21:14

The other context in which the word "null" may occur is in databases. In that case the significance is that the data item is not supplied (for some reason). In that case it needs to be different from 0. For example, "number of children" for a person; "unknown" is different from "none".
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Sep 24, 2009 21:40

The word NULL appears frequently in the Windows API documentation, typically as a value passed in a required parameter to indicate that the parameter should be ignored or that some default value should be substituted.
Posts: 135
Joined: Aug 29, 2008 21:13

Postby Hard » Sep 24, 2009 23:41

thanks to you all guys

the topic should be really clear now

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests