NULL values


This post is about the small things I know about NULL value used in C and C++.

First and foremost: NULL is a macro (whateva the hell does it mean!)

NULL is actually used to check if a pointer is valid or not. Now, if you are aware of pointers, then you must be knowing that pointers actually store the address of variables in a memory. Since, they are storing the address, their count starts from 1(01,001,00001,00…001). The upper limit is memory dependent. For eg. for a memory of 1024 kb, the pointers value can maximum be= 1024*1024.

Hence, in c++ it is taken to be 0 in value. That is why, you may see in some sources it typecasted as (int)NULL to get value 0. Gross huh.!

NULL is not at all a thing to fear of. Try some statements like:

char *a;
if(a==NULL) {…}
int b=(int)NULL;

Actually, when I checked, I was getting following warnings in CodeBlocks:

converting to non-pointer type `char’ from NULL
NULL used in arithmetic

But actually speaking, they were just warnings. Try to avoid NULL, but when dealing with pointers, you cannot escape it. Moreover, it makes your code more readable and understandable.

Cheers and Happy codeing..!

Advertisements
Explore posts in the same categories: C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: