Why are there different types of pointers for different data types in C?

前端 未结 7 1345
难免孤独
难免孤独 2020-12-03 15:08

If we have to hold an address of any data type then we require a pointer of that data type.

But a pointer is simply an address, and an address is always int

7条回答
  •  情书的邮戳
    2020-12-03 15:44

    There are several reasons:

    • Not all addresses are created equal; in particular, in non Von Neuman (e.g. Harvard) architectures pointers to code memory (where you often store constants) and a pointers to data memory are different.
    • You need to know the underlying type in order to perform your accesses correctly. For example, reading or writing a char is different from reading or writing a double.
    • You need additional information to perform pointer arithmetic.

    Note that there is a pointer type that means "simply a pointer" in C, called void*. You can use this pointer to transfer an address in memory, but you need to cast it to something useful in order to perform operations in the memory pointed to by void*.

提交回复
热议问题