answersLogoWhite

0


Best Answer

A null pointer is a pointer that has been initialised with the NULL value (zero). An uninitialised pointer is one that has not be initialised to any value and will in fact store whatever value happened to reside in the pointer's own memory address at the point of instantiation. Uninitialised pointers are a clear sign of bad programming because the only safe way to determine if a pointer is valid or not is to compare its value with NULL. An uninitialised pointer will almost always be non-NULL, which means you run the risk of accessing memory that either does not belong to you, or is otherwise invalid. Most compilers include a debug switch to warn you when you attempt to access any uninitialised variable, which naturally includes pointer variables. This switch must be on at all times.


Whenever you instantiate a pointer, always initialise it straight away, either by nullifying it, or by storing a valid memory address in it. When you are finished with the pointer, it's good practice to nullify it immediately, even if the pointer would subsequently fall from scope. If the pointer is to be immediately re-assigned, there is no need to nullify it (but it's good practice nonetheless). If you follow this practice at all times, you can be assured that any non-NULL pointer will always be pointing at something valid (and if it isn't, then you have some serious problems elsewhere in your code).


As a rule of thumb, if you can use a reference rather than a pointer, use a reference. They are much easier to work with. Pointers should only be used if there's any possibility (however remote) that a reference could be NULL, because a NULL reference will completely invalidate your program (pointers can be NULL, but references can never be NULL). This is why functions such as malloc() and calloc(), and the C++ new operator all return pointers rather than references. You can only return a reference when an allocation is guaranteed to succeed, and a dynamic memory allocation simply cannot make that guarantee.


User Avatar

Wiki User

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

Wiki User

11y ago

A NULL pointer is a pointer variable that stores the value zero. Although there is a memory address at offset 0, this value is reserved to indicate that the pointer is not pointing at any valid memory.

A void pointer is a pointer variable that has no specific type. For instance, int * p; tells the compiler that p will point to a memory location that will be treated as if it were an int type (even if it is a char, or some other type), whereas void * p; tells the compiler that the memory being pointed at has no specific type, and that the type will be determined at runtime by casting the pointer to the appropriate type. This allows the pointer to point to anything, including NULL. An int pointer can also point at anything, but its type is known at runtime and will be treated as such at runtime. The type of a void pointer can only be determined at runtime.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Unitialized pointer may contain any value, so if you should use them, the results are unpredictable; NULL value on the other hands explicitly means that the pointer does not point anywhere.

e.g.:

size_t ListLen (const List *p)

{

size_t len= 0;

while (p != NULL) {

++len;

p= p->Next;

}

return len;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An uninitialized pointer is a pointer that has not been assigned a value; it's uninitialized.

The value of an uninitialized pointer is undefined (it could be anything).

A null pointer is simply a pointer that has a value of NULL.

A word of advice: initialize your pointers to NULL.

This allows you to see that the pointer is invalid; to check if an uninitialized pointer is valid is quite impossible.

Examples:

Uninitialized:

int *myPointer;

Null-pointer:

int *myNullPointer = NULL;

Dereferencing either of these pointers is something you'll like to avoid.

It is very common in a program to check for null pointers; some functions return NULL on failure, for example:

struct Foo *result = doSomething();

if (result == NULL) {

printf("Error: The function doSomething() returned NULL);

return;

} else {

printf("The result: %d", result->bar);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An uninitialized pointer is the same as any other uninitialized variable. It has no definite value. Using (or dereferencing) an uninitialized pointer can result in bad results or damage to some unknown area of the program, data, or operating system, or it can result in a bus fault.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

An uninitialised pointer is a pointer that has been instantiated but has not yet been assigned a valid memory address. If you attempt to dereference whatever address happens to exist in the pointer when it was instantiated, then undefined behaviour will result. This is no different to using any other type of variable that has not yet been initialised.

A null pointer is a pointer that has been assigned address zero, which simply means "does not point at any address".

int *p; // uninitialised pointer -- do not dereference until initialised!!

p = 0; // ok -- points at nothing

p = malloc (sizeof(int)); // ok -- holds a valid address or zero if the allocation failed

Note: if the result of a malloc is address 0, it means the allocation failed, but the pointer is in a valid state. You should always check a pointer is non-null before attempting to dereference the pointer.

This answer is:
User Avatar

Add your answer:

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

Trying to delete uninitialized pointer to object?

You cannot delete an uninitialized pointer, because there is no allocation for the object, and the pointer contains garbage. That includes the case where you attempted allocation and failed, but deletion is safe in that case because a NULL pointer is "safe" to delete, even though it does not point at anything.


What is the difference between structure and pointer?

A structure is a collection of primitives or other structures. A pointer is a memory address. Comparison of the two is like comparing bowling balls to cinder blocks. You can say that a structure defines the layout of the data, while a pointer points to data that is a particular structure.


What is the difference between a function pointer and a pointer to a function?

A pointer to a function is the memory address that stores the address of a function, while the pointer itself is a function pointer.A pointer to a function might be defined as "int (*pf)(int, int);", while to actually point to the function, you would use a function pointer, such as "pf = &func;".


In which language pointer concept is not used?

Java


Different types of pointers in c language?

... are usable. void pointer (generic pointer) : a special type of pointer which point to some data of no specific types. void *p; null pointer : a special type of pointer which point nowhere. it is usually used to check if a pointer is pointing to a null or free the pointer during deallocation of memory in dynamic memory allocation; it is define by using the predefine constant NULL int *p=NULL; wild pointer : uninitialized pointer. it hold a garbage value. i.e it is not pointing to any memory location yet. dangling pointer: pointer pointing to a destroyed variable. it usually happen during dynamic memory allocation when the object is destroyed but not free and the pointer is still pointing to the destroy object.

Related questions

Trying to delete uninitialized pointer to object?

You cannot delete an uninitialized pointer, because there is no allocation for the object, and the pointer contains garbage. That includes the case where you attempted allocation and failed, but deletion is safe in that case because a NULL pointer is "safe" to delete, even though it does not point at anything.


A c program to show the drawbeck of pointer?

int main (void) { int *p; /* uninitialized pointer */ *p = -1; /* writing to random memory */ return 0; }


Difference between pointer to constant and constant pointer?

1. pointer to a constant means you can not change what the pointer points to 2. constant pointer means you can not change the pointer.


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 genric pointer and normal pointer?

Generic pointer of type 'void *' is compatible with any (data-)pointer, but you cannot use the following operators on it: + - ++ -- += -= * -> []


Give the difference between function and pointer in c?

There is no similarity between the two.


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 the difference between structure and pointer?

A structure is a collection of primitives or other structures. A pointer is a memory address. Comparison of the two is like comparing bowling balls to cinder blocks. You can say that a structure defines the layout of the data, while a pointer points to data that is a particular structure.


What is the difference between a power point and a white pointer shark?

dfs sd


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 the difference between a function pointer and a pointer to a function?

A pointer to a function is the memory address that stores the address of a function, while the pointer itself is a function pointer.A pointer to a function might be defined as "int (*pf)(int, int);", while to actually point to the function, you would use a function pointer, such as "pf = &func;".


Difference between gui and non-gui modems?

the difference between the gui & console are ,in the gui we can use the mouse pointer and console screen only we have to used the character. from anknush