声明指针类型? [英] Declaring type of pointers?

查看:95
本文介绍了声明指针类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才读,我们需要指点的类型,而在C(或C ++)即宣布他们:

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 store the address of variables, and address occupies same amount of memory whatever may be the type. So, why do we need to declare its type?

推荐答案

类型安全。如果你不知道什么是 P 应该指向,那么会是什么,以prevent类错误,如

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;

静态类型检查是preventing各种错误这样的一个非常强大的工具。

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

C和C ++也支持的指针运算的,如果目标类型的大小是已知的才有效。

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

这对当今流行的平台上实现。但已经有平台的量,这是并非如此。例如,一个指针指向一个多字节字可能比一个指向一个字节较小,因为它不需要重新present字节的字内的偏移

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.

这篇关于声明指针类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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