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

后端 未结 9 631
無奈伤痛
無奈伤痛 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: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.

提交回复
热议问题