What is the difference between a null pointer and a void pointer?

What is the difference between a Null pointer and a Void pointer?

+58
c pointers
Aug 27 2018-10-10T00:
source share
12 answers

A Null pointer is a special reserved value for a pointer. A pointer of any type has such a reserved value. Formally, each concrete type of pointer ( int * , char * , etc.) has its own null pointer value. Conceptually, when a pointer has this null value, it does not point anywhere.

The Void pointer is a specific type pointer - void * is a pointer that points to some data location in the storage that does not have a specific type.

So again, the null pointer is the value , and the void pointer is . These concepts are completely different and not comparable. This essentially means that your question, as indicated, is not entirely correct. This is similar to a query, for example: "What is the difference between a triangle and a car?".

+109
Aug 27 2018-10-10T00:
source share

These are two different concepts: "void pointer" is a type (void *). "null pointer" is a pointer that has a value of 0 (NULL). Example:

 void *pointer = NULL; 

NULL pointer void.

+11
Aug 27 '10 at 5:41
source share

The null pointer is not guaranteed to be compared with a pointer to any object. Actual value may vary by system and may vary by type. To get a null int pointer, you would do

 int* p = 0; 

A null pointer will be returned by malloc on error.

We can check if the pointer is null, i.e. if malloc or some other function failed to simply check its boolean value:

 if (p) { /* Pointer is not null */ } else { /* Pointer is null */ } 

The void pointer can point to any type, and you decide how much memory the objects referenced by the objects dereference and pointer arithmetic.

+4
Aug 27 '10 at 5:45
source share

Void is a type. In principle, the type of data to which it points is unknown.

Null refers to a value. This is essentially a pointer to nothing and is unacceptable to use.

+3
Aug. 27 2018-10-10T00:
source share

Points with a null pointer are NULL , which is usually 0, but in any case, a memory location that is not valid for dereferencing. The void pointer points to data of type void. The word "void" is not a sign that the data referenced by the pointer is invalid or that the pointer has been invalidated.

+2
Aug 27 2018-10-10T00:
source share

Usually a null pointer (which can be of any type, including a void pointer!) Points to:

  • address 0, against which most sets of CPU instructions can perform very fast validation and branching (for example, to check for uninitialized or invalid pointers) with optimal code size / performance for ISA.

  • an address that is forbidden to access the user's code (for example, 0x00000000 in many cases), therefore, if the code is really trying to access the data at or near this address, the OS or the debugger can easily stop or with this error.

The void pointer is usually a method for checking the type of compilation or disabling, for example, if you want to return a pointer to one type or an unknown type to use as another type. For example, malloc () returns a void pointer to a piece of memory without a type, the type of which you can use for later use as a pointer to bytes, short ints, double floats, typePotato, or something else.

+1
Aug 27 '10 at 6:19 06:19
source share

NULL is a value that is valid for any type of pointer. This means no value.

The void pointer is a type. Any type of pointer is converted to a void pointer, so it can point to any value. This makes it useful for general storage, but bad for use. By itself, it cannot be used to access a value. A program must have an additional context in order to understand the type of value referenced by the void pointer before it can access the value.

+1
Aug 27 '10 at 6:28
source share

Null pointers and void pointers are completely different from each other. If we request the operating system (via malloc () in c langauge) to allocate memory for a specific data type, then the operating system allocates memory in the heap (if space is available on the heap) and sends the address of the allocated memory.

When memory is allocated os on the heap, we can assign this address value to any variable of pointer type of this data type. This pointer is then called the void pointer until it is accepted for any process.

When space is not available on the heap, the operating system, of course, allocates memory and sends the address value of this location, but this memory is not allocated on the os heap because there is no space on the heap, in this case this memory is allocated os in the system memory. This memory cannot be accessed by the user, therefore, when we assign this address value in a pointer, this pointer is known as a null pointer, and we cannot use this pointer. In the case of the void pointer, we can use it for any process in any programming language.

+1
Sep 10
source share

I do not think AnT answer is correct.

  • NULL is just a pointer constant, otherwise how could we ptr = NULL .
  • Since NULL is a pointer, what is its type. I think the type is simple (void *) , otherwise we can have both int * ptr = NULL and (user-defined type)* ptr = NULL . The void type is actually a generic type.
  • Cited in "C11 (ISO / IEC 9899: 201x) §6.3.2.3 Pointers Section 3":

    An integer constant expression with a value of 0 or such an expression cast from the void * type is called a null pointer constant

So just put: a NULL pointer is a constant of a void pointer.

0
Jul 28 '16 at 0:44
source share

A Null pointer is a pointer that does not point to anything; it is used in cases where the pointer does not have a valid address in memory. Each pointer type ie int *, char * has a null pointer value.

A void pointer is one that does not have a data type associated with it, that is, it can be assigned a value of any type. Also known as a general-purpose pointer, this is the C convention for a raw address.

So, a null pointer is basically a null value assigned to a pointer of any data type, whereas a void pointer is a data type that remains invalid if it is not assigned a data type address.

0
Jul 19 '17 at 14:35
source share

A null pointer is 0. A null pointer is a generic pointer introduced by ANSI. A universal pointer can contain the address of any data type.

0
Jun 20 '19 at 12:37
source share

The void and null pointers point to the memory area at the end.

null pointer indicates a location (in a memory segment), which is usually the 0th address of a memory segment. This is almost all about how the underlying OS processes this “reserved” memory area (implementation detail) when you try to access it for reading / writing or dereferencing. For example, this particular memory location may be marked as “inaccessible” and throws an exception when accessing it.

void pointer can point to any place and potentially represent any type (primitive, reference type, including null ).

As well stated above, a null pointer represents a value, whereas void* represents a type greater than a value.

0
Jul 6 '19 at 12:52 on
source share



All Articles