在 C 语言中,取消引用指向 int 的指针与取消引用指向 char 的指针是否相同?

发布于 2024-12-10 19:18:29 字数 454 浏览 0 评论 0原文

假设在这种情况下,您想要在取消引用指针到指针后存储一个地址。从功能上和任何其他方面来说,将其取消引用为:

    *(int **)(/* some void ptr */) = // some other address;

vs

    *(char **)(/* same void ptr */) = // some other address;

因为我知道当您仅取消引用一个指向 int 的指针和一个指向 char 的指针时,它们是完全不同的东西!但是,在处理指向指针的指针时,中间有一层,并且由于所有指针(在同一台机器上)的大小相同,我想知道这两种方法是否有任何区别

-> 编辑:从字面上看,您甚至可以将其替换为 unsigned ptr ptr、long ptr ptr 和 void ptr ptr,并得到相同的结果吗?

Let's say in that situation you are wanting to store an address after you dereference your pointer to pointer. Does it make any difference, functionally and in any other way, to dereference it as:

    *(int **)(/* some void ptr */) = // some other address;

versus

    *(char **)(/* same void ptr */) = // some other address;

Because I know when you dereference just a pointer to an int, and just a pointer to a char, they are completely different things! But when dealing with pointers to pointers, there is a layer in between, and since all pointers (on the same machine) are of the same size, I was wondering if there was any difference at all in those two approaches

->edit: Literally could you even replace it with unsigned ptr ptr, long ptr ptr, and void ptr ptr, and get the same thing?

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

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

发布评论

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

评论(3

酷炫老祖宗 2024-12-17 19:18:29

您的假设“所有指针(在同一台机器上)都具有相同的大小”是不正确的。对于您的特定机器来说,这很可能是正确的,并且在您可能使用的任何机器上,它很可能是正确的,但 C 标准没有做出这样的保证,并且在现实世界的机器中,情况并非如此。例如,在字寻址机器上,int* 可能表示为字指针,但 char*void* 可能表示为字指针。需要额外的信息来指定它引用的字中的哪个字节。在某些体系结构上,函数指针与数据指针完全不同。

如果您发现自己正在转换指针类型,则您的设计可能有问题,并且指针首先应该是正确的类型。

当然,情况并非总是如此。在某些情况下,这种低级访问是适当且必要的,而 C 的强大优势之一就是它允许您执行此类操作。

但如果没有更具体的例子,很难判断它是否合适。

具有不同类型的要点不仅在于它们具有不同的大小或表示形式;还在于它们具有不同的大小或表示形式。关键是它们用于不同的目的。 int* 指向 int; a char* 指向一个char

您的示例之间的区别在于,一个引用 int* 对象,另一个引用 char* 对象。它们是不同的类型,并且不能互换,即使它们碰巧具有相同的大小和表示形式。作业右侧注释掉的其他地址必须是某种实际的表达式,并且必须是某种类型。

你想实现什么目标?

Your assumption that "all pointers (on the same machine) are of the same size" is incorrect. It may well be true for your particular machine, and it's fairly likely to be true on any machine you're likely to use, but the C standard makes no such guarantee, and there have been real-world machines where it's not true. For example, on a word-addressed machine, an int* might be represented as a word pointer, but a char* or void* might require additional information to specify which byte within a word it refers to. And function pointers, on some architectures, are quite different beasts than data pointers.

If you find yourself casting pointer types, it's likely that there's something wrong with your design, and the pointer should have been of the correct type in the first place.

That's not always true, of course; there are case where that kind of low-level access is appropriate and necessary, and one of C's great strengths is that it allows you to do that kind of thing.

But it's difficult to tell whether it's appropriate without a more concrete example.

The point of having distinct types isn't just that they have different sizes or representations; the point is that they're used for different purposes. An int* points to an int; a char* points to a char.

The difference between your examples is that one refers to an int* object, and the other refers to a char* object. They're different types, and they're not interchangeable, even if they happen to have the same size and representation. Your commented-out some other address on the right hand side of the assignment has to be some actual expression, and it has to be of some type.

What are you trying to accomplish?

强辩 2024-12-17 19:18:29

您可以将指针转换为您想要的任何其他类型。在大多数现代实现中,无论数据指向什么,指针类型的大小与任何其他指针类型相同。

然而,C++ 在类型正确性方面比 C 更严格,因此如果您盲目地将一种类型的指针分配给另一种类型,您可能会看到“无效转换”错误。

You can cast a pointer to be whatever other type you'd like. In most modern implementations, the pointer type is of the same size as any other pointer type, regardless of what the data is pointing to.

However, C++ is stricter than C when it comes to type correctness, so you'll probably see an "invalid conversion" error if you were to blindly assign the pointer of one type to another one.

陌路黄昏 2024-12-17 19:18:29

是的,除了有趣的内存模型之外,所有尺寸都相同 - 指针的尺寸。是的,你会得到同样的东西。

Yep, all the same size - size of the pointer, funny memory models aside. And yes you will get the same thing.

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