Declaring type of pointers?

前端 未结 10 2087
花落未央
花落未央 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:49

    Type-safety. If you don't know what p is supposed to point to, then there'd be nothing to prevent category errors like

    *p = "Nonsense";
    int i = *p;
    

    Static type checking is a very powerful tool for preventing all kinds of errors like that.

    C and C++ also support pointer arithmetic, which only works if the size of the target type is known.

    address occupies same amount of memory whatever my be the type

    That's true for today's popular platforms. But there have been platforms for which that wasn't the case. For example, a pointer to a multi-byte word could be smaller than a pointer to a single byte, since it doesn't need to represent the byte's offset within the word.

提交回复
热议问题