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

后端 未结 9 617
無奈伤痛
無奈伤痛 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 01:02

    Please, tell us: whats the difference:

    • between gas tank and no-gas situation
    • between cookie jar and no-cookies
    • between term 'money' and 'empty pockets'

    If you come up with these, you'l be able to grasp null vs void* dillema.

    0 讨论(0)
  • 2020-11-29 01:06

    The linked article is simply wrong. Its first sentence:

    a pointer with no return type is called a null pointer

    is triggering all sorts of alarms for me. This is a highly confused piece of writing.

    You are almost correct. "Pointer to void" is a type (not a "return type"). Values of any type can be returned by functions, and thus be (the function's) return type.

    A null pointer is a pointer that, regardless of its type, is pointing at the null object, which is not any valid object that can be created. A null pointer can be said to point at "nothing".

    A pointer to void can also be null;

    void *nothing = 0;
    

    is perfectly valid code, and just says that this pointer is capable of pointing a an untyped object, but right now it isn't.

    0 讨论(0)
  • 2020-11-29 01:09

    null pointer is point to 0x000000(which is incorrect to access pointer), while void pointer is a correct pointer to an unspecified type(void *). However, void pointer can be null pointer, but then unreferencing the pointer will generate error.

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