What would be the problem in case NULL was defined as (char *) 0 in C Programming

CWC
3 Min Read

The problem

There is a major problem of using NULL in place of (char *) 0 in C programming because;

(char *) 0 is a pointer to the character at address 0. this does not mean that it is a NULL. You can set address 0 to be a representative of any other character and not necessarily a zero.Assuming that (char *) 0 is a NULL is misleading. What would then be the value of (char *) 1, (char *) 2 (char *) 3 or (char *) 4? This is a wrong assumption that does not have any consistency.Therefore, it suffices to say that indeed, NULL cannot be used for the syntax (char *) 0.

What is the real issue?

(char *) 0 is not necessarily a pointer to the character at address 0 but rather, designed to produce a NULL pointer value. In C programming, (char *) 0 does not necessarily correspond to the address at 0 but rather can stand in for any other characters located at different addresses. However, the official stand from C++ is that a null pointer value does not point to anywhere. It just points to a null (char *).

On the other hand, characters with a definite value do not have such special meanings. For example, (char *) 8 corresponds to the character at address 8. (char *) 6 represents the character at address 6 and so on.

However to work with a special character such as (char *) 0, you need to code it right so as to achieve its accurate meaning. You can try something like;

int k = 0;

(char *) k;

If put as the above, (char *) k will produce a character at address 0. The idea here is to remove 0 as a constant in the expression. However, understanding the principles that governs the use of C++ is a matter of looking at the most acceptable practices. For example, all users have to abide by the same programming rules. This can bring uniformity in the language and industry in general.

This will only work for char pointers, assigning NULL value to pointer to integer, etc will result in invalid pointer.
Also FILE *myFile = NULL will behave in undefined way.
double sum(double value,...) {
va_list valist;
double sum = 0.0;
int i, count = 0;
va_start(valist, count);
for (i = 0; i < count; i++) {
sum += va_arg(valist, double);
}
return sum;
}
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version