为什么不同类型的指针,在C不同的数据类型? [英] Why different types of pointer for different data type in c?

查看:156
本文介绍了为什么不同类型的指针,在C不同的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们要保存任何数据类型的地址,然后我们要求的数据类型的指针。
但指针只是一个地址,一个地址总是 INT 型,那么为什么任何数据类型的召开地址需要这种类型的指针?

If we have to hold an address of any data type then we require pointer of that data type. But a pointer is simply an address, and an address is always int type, then why does the holding address of any data type require the pointer of that type?

推荐答案

有几个原因:


  • 不是所有的地址都是一样的;特别是,在非冯 - 诺依曼(如哈佛)架构指向code内存(其中经常存储常量)和指针数据存储器是不同的。

  • 您需要知道为了正确地执行你的访问基础类型。例如,读取或写入字符是读取或写入不同的一个双击

  • 您需要更多的信息来执行指针运算。

  • 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.

请注意,有一个指针类型,这意味着在C只是一个指针,名为无效* 。您可以使用该指针在内存中传递的地址,但需要通过将其转换为有用的东西才能在内存中执行操作指向无效*

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*.

这篇关于为什么不同类型的指针,在C不同的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆