What is a void pointer and what is a null pointer?

后端 未结 9 616
無奈伤痛
無奈伤痛 2020-11-29 00:32

So I was going through some interview questions and I came across one about void and null pointers, which claims:

a pointer with no return type is cal

相关标签:
9条回答
  • 2020-11-29 00:48

    Here's some differences with respect to pointer arithmetic:

    It stems from the fact that void is an incomplete type.

    void *vp;
    vp++;     // error, incomplete type
    vp += 2;  // same error
    
    void *p = 0;
    p++;      // still same error
    
    int *p = 0;
    p++;      // well-formed program, but UB ($5.6/5)
    
    0 讨论(0)
  • 2020-11-29 00:49

    The void type in general means that no type information is given.

    You should always keep in mind that a pointer conveys two pieces of information: the type of the pointed data (int, double, ...), which specifies how to interpret it, and the address of the data it points to, which specifies where you can get the actual value of the pointed data.

    The type information is in the type of the pointer (double*, int*, ...), while the address of the data is the actual value contained in the pointer variable.

    So, a void pointer (void *) is a pointer that do not specify any type information. It tells you where the data is, but it doesn't tell you how to interpret it. You know that at that address there's something, but you don't know if it's an int, a double or an array of flying cows. To actually use such data, you have to get type information about it in some other way (e.g. with some other magic parameter), cast that pointer to a regular pointer type and then use it as usual.

    void * is often used in C to provide some kind of support to generic programming; see for example the qsort C library function.

    A NULL pointer, instead, is a pointer that points to nothing. In this case, the type information about the pointer in general is present, but it's the address of the pointed data that is missing. Of course, it's possible to have a void * that is NULL.

    Quick example (supposing that v is declared as double v;):

                             Type information present
                 +----------------------+----------------------+
                 |          ✔           |          ✘           |
             +---+----------------------+----------------------+
        p  c |   |                      |                      |
     v  o  o | ✔ | double * ptr = &v;   | void * ptr = &v;     |
     a  i  n |   |                      |                      |
     l  n  t +---+----------------------+----------------------+
     i  t  e |   |                      |                      |
     d  e  n | ✘ | double * ptr = NULL; | void * ptr = NULL;   |
        d  t |   |                      |                      |
             +---+----------------------+----------------------+
    

    Trivia: NULL, at least in the current standard, is guaranteed to be 0.

    In other areas of the language, void is always used to specify lack of type. Using it as return value (note: I'm talking now about void, not void *) means that the function does not return any value, and casting an expression to void is a fancy way to discard a value (you're signaling to the compiler and to other programmers that you're conscious that you're not using a certain value).

    0 讨论(0)
  • 2020-11-29 00:52

    The two concepts are orthogonal:

    1. A void pointer, (void *) is a raw pointer to some memory location.
    2. A null pointer is a special pointer that doesn't point to anything, by definition. It can be a pointer to any type, void or otherwise.

    A void pointer can be null or not:

    void *void_ptr1 = nullptr;
    void *void_ptr2 = malloc(42);
    void *void_ptr3 = new Foo;               // void * can point to almost anything
    void *void_ptr4 = (char*)void_ptr3 + 1;  // even somewhere inside an object
    

    A non-void pointer can also be null or not:

    Foo *f = nullptr;
    Foo *g = new Foo;
    
    0 讨论(0)
  • 2020-11-29 00:52

    A void *ptr is the pointer which can be used to point any type of data. It maybe int, float, double. It has no return type that is initially pointer is created with pointer type (having hex value) and we can assign this pointer to any type of data.

    While null pointer is the pointer with having NULL value as address, the pointer is assigned NULL value so that it cannot be used to access others data which its address may contain while creation. I think it is good programming technique to assign a pointer NULL if its not used at the moment.

    0 讨论(0)
  • 2020-11-29 00:58

    Just plain forget about that answer. A quote from your link :

    "a pointer with no return type is called a null pointer."

    This is sooo plain WRONG. A pointer's return type? REALLY? This is a bad source...

    void* is universal pointer type because any pointer type (except for pointer to const and/or volatile) can be implicitly converted to void*. In other words, you can assign any pointer to a variable of type void*. A null pointer is a pointer value 0

    0 讨论(0)
  • 2020-11-29 00:58

    void is a non-type. null is a non-value.

    0 讨论(0)
提交回复
热议问题