Why is the data type needed in pointer declarations?

前端 未结 8 662
萌比男神i
萌比男神i 2020-12-04 22:07

As far as I know about data types in C/C++, while declaring a variable, we need to declare its data type, which tells the compiler to reserve the number of bytes in the memo

8条回答
  •  [愿得一人]
    2020-12-04 22:51

    What size a pointer needs depends on the system you are using. On a x86_64 system the pointer size might by 64 bit.

    The reason why you need the data type for pointers is because the compiler has to know what the size of the memory cell is, among others, the pointer is pointing to. Also type safety cannot be ensured w/o the type. Also, you would have to typecast every pointer when accessing structures from the pointer.

    You also could use a void pointer and do everything by hand. But why should you want that?

提交回复
热议问题