Declaring type of pointers?

前端 未结 10 2061
花落未央
花落未央 2020-12-01 05:20

I just read that we need to give the type of pointers while declaring them in C (or C++) i.e.:

int *point ;

As far as I know, pointers sto

10条回答
  •  情深已故
    2020-12-01 05:40

    We actually don't need (see below) to declare the type, but we should. The pointer stores information about the objects location, while the type defines how much space it takes in memory.

    The size of the object stored at the pointed memory is needed in various cases - array creation, assigment, copying the memory, and finally - creating the object using new.

    However you can still define a void pointer, if you want to hide (for any reason) the type:

    void* dontKnowWhatTypeIsHere;
    

    A void pointer is considered an universal one. It can point to any object, and when we want to use it with a type, we'll just do reinterpret_cast.

提交回复
热议问题