answersLogoWhite

0


Best Answer
Using a NULL macro to make C portable

I'll assume that you're asking your question for C type language programming. A NULL pointer is a pointer that's guarnteed to point to nothing. This may be 0 in a UNIX/Linux system or some other address in another system. Using the NULL macro to set/initialize your pointers will make your programs more portable among systems than using something like the 0.

#include

char *c = 0; // initialize to NULL--not portable
char *p = NULL; // initialize to NULL as defined in stdio is portable


AddendumThe code:

char *c = 0;

actually is portable because the compiler converts 0's used in a pointer context (cast to a pointer) to the machine's representation of a NULL pointer, which may or may not be all 0 bits. The NULL macro itself might be defined as something like 0 or (void *)0, and both definitions are portable. As a corollary, the following code is also portable:

if (!c) {
// do something
}

because it is equivalent to:

if (c != 0) {
// do something
}

and the 0 above is converted to a NULL pointer because it is being compared with a pointer.
User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

A null pointer is a pointer variable with the value 0. A null macro is a macro that defines the NULL symbol (typically 0L on a 32-bit system).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between a null pointer and a null macro?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is null macro?

The NULL macro is an implementation-defined macro. It is used to symbolise the zero address (0x0) in C programs and older C++ programs. It is not type safe, but is the conventional method of assigning the zero address to a pointer variable. C++11 introduced the type-safe nullptr data type.


What is the difference between null pointer ASCII null character and null string?

A null pointer is a pointer which does not point to any valid memory location, and usually contains the binary value "0" to represent this (this is language dependent). The ASCII null character is a character-sized zero value (in ASCII, it is an unsigned byte with a value of 0), and typically represents the end of a string (esp. as in C and C++). A null string is one that is zero characters of usable string data; in a length-based string, this means the length parameter is set to 0, and in an ASCII null-terminated string, means the first character is set to 0.


When you let your pointer linger over an object the pointer is?

The pointer is non-NULL.


Java null pointer exceptions?

A null pointer exception in java comes when you are trying to perform any action on an object that isnt initialized/has a value i.e., is a NULL Value Ex: private String s; //declare a string if(s.equals("test")){ //do something.. } You will get a null pointer in the if condition because you are checking a value that is null which is not allowed..


What is the size of null pointer?

A NULL pointer has the same size as a non NULL pointer. NULL means that the pointer has been set to the NULL value that is usually zero (0) but the NULL value is at the digression of the compiler manufacture (and may have a value other than zero) so a pointer should always be set to the NULL value and not zero. Current compilers (32 and 64 bit, Intel chip) have a pointer size of 4 (8 bit) bytes. It should be noted that the number of bits in any data type is at the compiler manufactures digression but is heavily influenced by the computer hardware. void *p= NULL; printf ("%d\n", sizeof (p)); or printf ("%d\n", sizeof (void *));

Related questions

What is a null macro what is the difference between a null pointer and a null macro?

NULL Macro is simply what is defined as 0 in a macro provided by the libraryNull pointer is a pointer which has 0 or NULL value stored and points to nowhwere still it points to 0x00 i.e. the first memory location of the OSNull pointer != Uninitialized pointer because an uninitialised pointer can point anywhere in the memory location ...but a NULL pointer surely points to no where(but still behind the scene we can say that it only points to 0x00). Never we can retrive a Null pointer location using th"&" operator..neither will malloc/calloc return NULL IF THERE IS SPACE IN THE MEMORY. NULL pointer is unique !!nishantnitb@aol.com


Difference between void pointer and null pointer?

A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Before ANSI, char pointers are used as generic pointer. Generic pointer can hold the address of any data type. Pointers point to a memory address, and data can be stored at that address.


What is null macro?

The NULL macro is an implementation-defined macro. It is used to symbolise the zero address (0x0) in C programs and older C++ programs. It is not type safe, but is the conventional method of assigning the zero address to a pointer variable. C++11 introduced the type-safe nullptr data type.


What is null point?

A pointer variable which is declared but not initialized is called a NULL POINTER.ex: int *p;Please don't use the above. A NULL pointer is a specific value assigned to a pointer, just like any other value. NULL is a language-specific designation, and is guaranteed to be comparable to, unlike uninitialized variables, which can have any value.That is:int *a;int *b = NULL;int *c = (int *) malloc(sizeof(char));( a c) is NEVER true.NULL is a reserved word in most high-level languages, and indicates a specific value for assignment. It is commonly used to indicate that something has not yet been assigned a "real" value, or has had its contents deleted. It is an EXPLICIT value, and not just "undefined".In the context of pointers (which, remember, are really memory location addresses), a NULL pointer is one which has NO value, and thus does NOT point to any memory location. The difference between an uninitialized pointer and a NULL pointer is that most common languages do not specify what value an uninitialized pointer has upon creation (many, such as C, are assigned a random value), while a NULL pointer explicitly has NO value (which is the meaning of NULL).Many modern languages and compilers will assign NULL to a pointer upon initialization, but don't count on it. It is sloppy programming to do so, and can lead to many hard-to-find errors.


What is a NULL Macro?

# define pi 3.17 // tihs is a preprocessing directive macro. "pi" is called macro template " 3.17" is the macro value. means in entire program pi value is 3.17. if we declared like # define pi 0 means pi value is zero means null. so the macro template which carries a null value is called a NULL MACRO.


What is the use of null pointer?

Pointer is a variable that stores address of a variable . A NULL Pointer a pointer that doesn't point to anything, it is a literal zero .Some people ,notably C++ programmers, prefer to use 0 rather than NULL.


What is the difference between null pointer ASCII null character and null string?

A null pointer is a pointer which does not point to any valid memory location, and usually contains the binary value "0" to represent this (this is language dependent). The ASCII null character is a character-sized zero value (in ASCII, it is an unsigned byte with a value of 0), and typically represents the end of a string (esp. as in C and C++). A null string is one that is zero characters of usable string data; in a length-based string, this means the length parameter is set to 0, and in an ASCII null-terminated string, means the first character is set to 0.


When you let your pointer linger over an object the pointer is?

The pointer is non-NULL.


Why you get null pointer assignment error occurs in c?

You haven't assigned the pointer yet, so it's initialized as NULL, or you're trying to assign NULL to the value of the pointer. You have to check if the value is NULL before you use it, or you'll end up with errors just like this.


Java null pointer exceptions?

A null pointer exception in java comes when you are trying to perform any action on an object that isnt initialized/has a value i.e., is a NULL Value Ex: private String s; //declare a string if(s.equals("test")){ //do something.. } You will get a null pointer in the if condition because you are checking a value that is null which is not allowed..


What is the difference between equals 0 and equals NULL?

NULL is for pointers, 0, for numbers


What is the difference between 0 and null?

Nothing - 0, Zero and null are the same things