了解结构对等 [英] Understanding Structural Equivalence

查看:88
本文介绍了了解结构对等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型的结构对等概念,我正在努力理解。

I have two types of structural equivalence ideas I am struggling to understand.

VAR_1 = int
VAR_2 = pointer to VAR_1

所以在这里,我觉得它们在结构上是等效的,因为从技术上讲类型都是指向的

So here, I feel like they are structurally equivalent because the types would technically be both pointing to an integer type, correct?

,但是如果您有类似的东西

but then if you had something like

VAR_3 = pointer to int

指向int与整数声明的指针是否相等?

Does the pointer to an int versus an integer declaration make them inequivalent?

我认为VAR_1和VAR_2在结构上是等效的,因为它们以正确的顺序包含相同的数据,但是VAR_3失败了,因为它不必是指针了? / p>

I am thinking that VAR_1 and VAR_2 are structually equivalent because they contain the same data in the correct order, but VAR_3 fails because it would need to not be a pointer?

推荐答案

晚些时候参加聚会,但我只想声明我不同意其他答案。

Late to the party, but I just want to state that I disagree with the other answer.

虽然语言实现的细节很重要,但结构上的等效最终不只是分配给类型的字长。如果是这种情况,您甚至可以争辩说 float int 在C中可以在结构上等效,因为某些C编译器可能使用相同长度的单词来实现它们。

While language implementation detail is important, ultimately structural equivalence is NOT just about the word length assigned to the type. If that was the case, you could even argue that float and int can be structurally equivalent in C since maybe some C compiler implements them using words with the same length.

如果您仍然不相信,请考虑将 pointer-to-int 相对于 int 递增时会发生什么

If you are not still convinced, think about what happens when you increment a pointer-to-int versus an int.

int p=100;
int *q=100;
p++;    //p = 101
*q++;  // Pointer moves to the next int in memory adding int word length to the underlying stored value

实际上,有人认为类型系统与语言语义有关。 Pointer-to-int 语义与C语言中的 int 语义不同。因此,这两种类型在结构上并不等效。

I would in fact argue that a type system has much more to do with language semantics. Pointer-to-int semantics is different than int semantics in C. Therefore these two types are not structurally equivalent.

要回答您的问题,VAR_1和VAR_2在结构上并不等效(一个是int,另一个是指向int的指针)。但是VAR_2和VAR_3在结构上是等效的。由于它们指向C中相同的基本类型。

To answer your question, VAR_1 and VAR_2 are not structurally equivalent (one is int, the other is pointer to int). But VAR_2 and VAR_3 are structurally equivalent. Since they point to the same basic type in C.

这篇关于了解结构对等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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