0 == NULL?

Posted By: Scorpion

0 == NULL? - 09/19/08 17:59

I got a bit confused why the engine does not differ between 0 and NULL. Is there a reason for it? Because it is a lot more handy if you can distinguish between these things. As example if you want to determinate, that a variable didn't get value OR has the value 0.

It would be great if that can be added.

Thank you
Scorpion
Posted By: Spirit

Re: 0 == NULL? - 09/19/08 18:03

Whats the difference between 0 and NULL? I thought its just two ways to express the same thing.
Posted By: Michael_Schwarz

Re: 0 == NULL? - 09/19/08 18:08

0 is the integer zero, indicating a variable, integer, double (...)

Imagine 0 as the byte "00000000"

NULL is a pointer to a zero memory address that contains... well, Nothing. NULL is neither "0" nor has *any* value.
Posted By: Spirit

Re: 0 == NULL? - 09/19/08 18:11

No, NULL is not a pointer to a zero memory address. In C/C++ NULL is a constant with the value 0x00000000. Which is the same as 0. Thats why I didnt understand what Scorpion meant.
Posted By: Michael_Schwarz

Re: 0 == NULL? - 09/19/08 18:14

it may be in C-Script, but in many other languages the comparison

if(0 == NULL)
{
echo "The same";
}

will never be true.
Posted By: Lukas

Re: 0 == NULL? - 09/19/08 18:14

litec.h:
#define NULL 0
Posted By: Spirit

Re: 0 == NULL? - 09/19/08 18:16

if (0 == NULL)

is the same as

if (0 == 0)

and always true in any language. Try it.
Posted By: TWO

Re: 0 == NULL? - 09/19/08 21:20

Python: 0 == nil -> false

As you can see nil is a special type in python and not equal 0 to indicate that an object is invalid. And maybe in many other script languages too, because it's a handy feature.

NULL is just an old C-Lang thing. I use always 0 in my code.
Posted By: Michael_Schwarz

Re: 0 == NULL? - 09/19/08 21:35

c#

0 == null > false

php

0 === null > false
Posted By: Scorpion

Re: 0 == NULL? - 09/19/08 21:36

Well I think I posted this a bit too fast. I looked it up and it's really defined as 0 (or in C: (void*)0 ). So I was misinformed there.

But then may I ask the question why that NULL exists? When is/was it used for which purpose?
Posted By: TechMuc

Re: 0 == NULL? - 09/19/08 21:42

(I'm really intersted: why do you actually need the strict difference between 0 and NULL? any important reason?)

definition of NULL in stdlib.h (how it is defined in c++ and c)

Quote:

#ifndef NULL
#ifdef __cplusplus
#define NULL 0 //NULL ist das gleiche wie 0
#else
#define NULL ((void *)0) //0 pointer
#endif
#endif


answer to the same question in an english forum

(http://bytes.com/forum/thread61514.html)
Quote:

In 'C', NULL is defined like this:

#define NULL (void*)0


In C++, NULL is defined like this:

#define NULL 0


If you try and initialise a pointer to a type with a pointer to void, you'll
get errors with a C++ compiler:

int* p = (void*)0; // OK in 'C*, error in C++
int* p = 0; // OK in C++, error in 'C'


C++ specifically forbids the 'C' style definition of NULL. Why? Perhaps
assigning a 'pointer to a type' to the 'C' style NULL could be considered
similar to assigning a 'pointer to a derived type' to a 'pointer to base
type', which would be wrong. Then again, is assigning a 'pointer to a type'
to the value zero is any more correct; besides which 'void' is not the base
type of all types? You could say that it's a bit of shambles (I do). Then
again, C++ isn't a real OO language; it's a baggage of OO extensions to 'C'
which is, essentially, just portable assembler. To do the job properly, you
would need a language that had a common base type for all types and Null
instances of every derived type.




just search the web.. you'll get a lot of answers smile
http://www.google.de/search?rlz=1C1CHMG_deDE291&sourceid=chrome&ie=UTF-8&q=NULL+C%2B%2B

PS: Again: why do you really care about the difference of NULL and 0?
Posted By: Scorpion

Re: 0 == NULL? - 09/19/08 21:49

*ganz schnell alle NULLen in den Keller verbann und nie wieder raushol*

imo wäre es aber trotzdem sinnvoll zu sehen ob einer variable ein 'benutzbarer' Wert zugewiesen wurde oder nicht.(das war jetzt kein 'neues-feature-vorschlag') In meinem Fall bin ich jetzt auf eine negative Zahl ausgewichen...aber auch das ist ja nicht immer möglich
Posted By: TechMuc

Re: 0 == NULL? - 09/19/08 21:52

Was meinst du mit "benutzbarer Wert"? Magst du checken ob ein Pointer initialisiert wurde? Also z.B.

int*a = 0;

? Falls ja kannst du if(a != 0) oder if (a != NULL) beides abfragen.. Zumindest in C++ gibt sich das nichts.
Posted By: Scorpion

Re: 0 == NULL? - 09/19/08 22:23

nicht direkt...uhm.. als beispiel: du hast eine variable für ein Objekt. Solange dieses Objekt nicht geladen ist ist sie 'undefiniert'. Sobald es dann geladen wurde, bekommt diese einen zu speichernden Wert (z.B. ID, oder eine vom Benutzer festgelegten Wert etc.). Dieser könnte dann auch 0 sein, was so nicht geht.
Da wenn jetzt die variable 0 ist könnte das a)undefiniert sein oder b)den Zahlenwert 0 darstellen.

Natürlich kann man das mit einer 2. Variable umgehen oder u.U. auch mit einer dezimalstelle etc. Das ist aber schade, da bei floats auch 'unendlich' oder 'NaN' angegeben werden kann.
Posted By: Spirit

Re: 0 == NULL? - 09/20/08 09:40

Not to be nitpicking but NULL is not the same as nil or null, nil is a real type, while NULL is a #define. So, NULL == 0 but NULL != nil != null.

For checking if a pointer is set to something you can use NULL for initializing it:

void* ptr = NULL;

and then:

if (ptr) // true if ptr was set to something, otherwise false
...
Posted By: FBL

Re: 0 == NULL? - 09/20/08 10:49

NULL ist eigentlich dafür gedacht, um Poitner zu leeren bzw. leere Pointer zu erkennen. Da man bei Pointern mit Adressen abreitet, mcht es Sinn, eine Art leere Adresse (was in Realität einfach Adresse 0x00000000 ist) irgendwie greifbar zu machen.

Wennman bei einer Variablen einen Leerwert, Fehlerwert etc haben will, baut man sich halt einfach ein define.

#define ERROR -1
#define EMPTY 0

was auch immer...
© 2024 lite-C Forums