Difference amongst macro NULL an '\0' character

Mar 15, 2012 at 5:14pm
Hello! Hope all of you are fine.

1. Can anyone explain to me, what is the difference between NULL and '\0' (which is also referred to as NULL character)?
- I understand that NULL is a macro defined in iostream, but what is its mechanism. - I mean if NULL value is assigned to a container it stores that particular container's default value in it. So what is it, have the defined multiple macros with the same name NULL?

2. Against this syntax char* temp = NULL; the pointer 'temp' starts pointing towards NULL. Is there any physical existence of NULL on our main memory?
- Or it's just that our pointer is not pointing anywhere at the moment?
- I am curious because when I try to dereference pointer(which is pointing to NULL currently) using the syntax cout<<*temp; my program malfunctions and crashes down.

3. What's the difference amongst following three statements?
1
2
3
char *temp = NULL;
char *temp = '\0';
char *temp = "";
Last edited on Mar 15, 2012 at 5:26pm
Mar 15, 2012 at 5:26pm
closed account (zb0S216C)
The Encapsulator wrote:
Can anyone explain to me, what is the difference between NULL and '\0' (which is also referred to as NULL character)?
'\0' is interpreted as a magic constant, which is zero. The definition of NULL depends on the compiler. I've seen two ways NULL was defined:

1
2
3
4
/* GCC: */
#define NULL 0
/* Visual C++: */
#define NULL ( ( void * )0 ) 

In the context of pointers, if the expression evaluates to zero, it pretty much means null. Of course, C++11 defines nullptr which should be preferred.

The Encapsulator wrote:
Against this syntax char* a = NULL; the pointer of type a starts pointing towards NULL. Is there any physical existence of NULL on our main memory?

I believe it points to the first address (0x0) in memory. Though, this will need to be confirmed. I mean, it has to point somewhere, right?

The Encapsulator wrote:
What's the difference amongst following three statements?
1
2
3
char *temp = NULL;
char *temp = '\0';
char *temp = "";

The first two are null, the 3rd points to a string with 1 character - the null character. This doesn't make the pointer null.

Wazzak
Last edited on Mar 15, 2012 at 5:29pm
Mar 15, 2012 at 5:27pm
They are pretty much just different ways of doing the same thing. I think NULL is the more commonly used though.
Mar 15, 2012 at 5:43pm
Thanks for your quick reply, though I still have a little ambiguity regarding the difference amongst '\0' and NULL macro. Can you please elaborate it a little further?

Framework wrote:
I believe it points to the first address (0x0) in memory. Though, this will need to be confirmed.

Yes, I mean what's the guarantee that location (0x0) is empty. So I think there must be some other concept involved.
Last edited on Mar 15, 2012 at 5:43pm
Mar 15, 2012 at 5:52pm
closed account (zb0S216C)
The Encapsulator wrote:
though I still have a little ambiguity regarding the difference amongst '\0' and NULL macro. Can you please elaborate it a little further?

Ultimately, NULL is zero. Both are magic constants (literals) that evaluate to zero.

Yes, I mean what's the guarantee that location (0x0) is empty.

I'm not entirely sure, actually. Maybe it's an absolute address reserved specifically for pointers? Though, this could be dependent on the implementation.

Wazzak
Mar 15, 2012 at 5:58pm
Yes, I mean what's the guarantee that location (0x0) is empty.

It's not so much that it's empty as that the OS will refuse to let you deference it or write to that location. It's possible under Linux (and presumably other OSes) to change things so that the address 0 does actually point into (permitted) memory.

I only do that to mess with people's debugging :p
Mar 15, 2012 at 7:43pm
Topic archived. No new replies allowed.