[pmwiki-devel] Testing empty string (was:php logic question)
christian.ridderstrom at gmail.com
christian.ridderstrom at gmail.com
Wed Mar 14 07:59:48 CDT 2007
On Wed, 14 Mar 2007, Hans wrote:
> Wednesday, March 14, 2007, 9:00:29 AM, christian wrote:
>
>> but as it's not NULL
>
> BTW, what is the difference between NULL and "" (and 0 for that matter)?
Historically speaking, i.e. the language C, NULL and 0 are the same.
That's for C though, it's probably different in other languages.
In C, it's used with pointers to signify that the pointer is invalid. If
you for instance requested some memory using
char *p; /* Declare a pointer to a character */
p = malloc(100); /* Try to allocate some memory */
/* Check if we got the memory */
if(p == NULL) { /* Error */ }
free(p); /* Release the memory */
In C, NULL and 0 are equivalent, so it's ok[*] to write
if(p == 0) { /* Error */ }
To confuse things, strings are byte sequences that are terminated by the
byte 0. :-) So an empty string in C would be a pointer (non-zero!) that
points to a byte that is zero.
As a curiosity, the memory area with offset (0) when a C-program runs
typically contains four zeros. The reason is so that the program can
detect if (non-zero) data has accidentally been written using a NULL
pointer.
/Christian
[*] Some older compilers and memory models (think DOS!) might require '0L'
for a long zero instead of just '0', depending on the exact type of your
pointer (near, far, huge), but this is getting into the details.
--
Christian Ridderström, +46-8-768 39 44 http://www.md.kth.se/~chr
More information about the pmwiki-devel
mailing list