int *p 应该改为 long int *p 吗?

发布于 2024-11-07 19:08:17 字数 203 浏览 0 评论 0原文

我通过阅读 K&R 并做练习来学习 c。我现在正在阅读第 5 章,其中涉及指针。我不明白为什么这个语句:

int *p;

不是:

long int *p;

因为 *p 包含一个地址,并且不能保证 int 类型的变量足够大以容纳一个大地址。或者有吗?

I'm learning c by reading K&R and doing the exercises. I'm now in chapter 5 which deals with pointers. I don't understand why the statement:

int *p;

is not:

long int *p;

since *p contains an address and there is no guarantee that a variable of type int will be large enough to hold a large address. Or is there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾北清歌寒 2024-11-14 19:08:17

int 是指针指向的对象的类型,而不是指针本身的大小。指针的大小与其指向的对象无关

例如:

int *p;

double *d;

指针 p 和 d “通常”具有相同的大小,但它们指向的数据不具有相同的大小尺寸。

编辑:正如评论中所指出的,指针实际上并不“要求”具有相同的大小。

正如约翰所解释的:

例如,一个字符
* 在字寻址系统上实际上可能比 int * 大,
因为它需要指定一个偏移量
进入这个词。唯一的保证是
void * 和 char * 具有相同的
对齐和表示,即
指向兼容类型的指针有
相同的对齐和表示,
指向结构类型的指针具有
相同的对齐方式和表示方式,以及
指向联合类型的指针都有
相同的对齐和表示

int is the type of the object that the pointer points to and not the size of the pointer itself. The size of the pointer is independant of the object it points to

For example:

int *p;

double *d;

Both pointers p and d would "normally" have the same size, but the data that they point to don't have the same size.

Edit: As pointed out in the comments pointers aren't actually "required" to be of the same size.

As John explained:

For instance, a char
* on a word-addressed system may actually be larger than an int *,
since it needs to specify an offset
into the word. The only guarantees are
that void * and char * have the same
alignment and representation, that
pointers to compatible types have the
same alignment and representation,
that pointers to struct types have the
same alignment and representation, and
pointers to union types all have the
same alignment and representation

晚风撩人 2024-11-14 19:08:17

long int * 表示指向 long int 的点。
这并不意味着长指针。

指针是独立的东西,并且有自己的大小。 (取决于您要编译的位数)

long int * means a point to a long int.
It doesn't mean a long pointer.

Pointers are separate things and will have their own size. (dependent on the bitness that you're compiling for)

春风十里 2024-11-14 19:08:17

int * 不是 int 类型。它是一个指针类型。因此它足够大以存储任何地址。 long int *int * 的大小相同——只是您将所指向的内容视为不同的。

int * is not of type int. It is a pointer type. Therefore it will be large enough to store any address. long int * is the same size as int * -- it's just that you're treating where things are pointed to as different.

猫七 2024-11-14 19:08:17

这是一个指向 int 的指针(我倾向于写为“int* p”)。这基本上意味着 p 是一个指向 int 的指针。

第二个是指向 long int 的指针。

它们都是指针,因此存储字节数相同,但一个引用 int,另一个引用 long int。

This is a pointer to an int (I tend to write as 'int* p'). This basically means that p is a pointer to an int.

The second is a pointer to a long int.

They are both pointers, and therefore the same number of bytes of storage, but one references an int, the other a long int.

忘东忘西忘不掉你 2024-11-14 19:08:17

不要将指针的类型与其所指向的类型混淆。 表达式 *p 的类型是 int,但 p 的类型是 int *< /代码>。

Don't confuse the type of the pointer with the type of what it points to. The type of the expression *p is int, but the type of p is int *.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文