理解 C 中结构体的流程

发布于 2024-12-06 10:30:15 字数 221 浏览 1 评论 0原文

我正在尝试了解 C 中的结构如何工作。我熟悉 Java 中的构造函数。现在,我有一个使用结构在 C 中创建树的示例。

struct a_tree_node{
      int value;
      struct a_tree_node *leftPTR, *rightPTR;
};

我目前正在尝试想象它是如何工作的,我有点困惑,因为这个结构包含它自己。

I am trying to learn how structs work in C. I am familiar with constructors in Java. Now, I have an example of creating a tree in C with structs.

struct a_tree_node{
      int value;
      struct a_tree_node *leftPTR, *rightPTR;
};

I am currently trying to visualize how this works, I am a little confused because this struct contains itself.

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

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

发布评论

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

评论(10

云朵有点甜 2024-12-13 10:30:15

我有点困惑,因为这个结构包含它自己。

该结构不包含其自身,而是包含指向同一类型结构的两个指针。这是理解的关键点。

包含自身的结构将是无意义的,并且无法编译,因为它是无限递归依赖项。

I am a little confused because this struct contains itself.

The struct doesn't contain itself, but rather two pointers to the same kind of structure. That's the key point to understand.

The struct containing itself would be nonsense and wouldn't compile because it's an infinitely recursive dependency.

原谅我要高飞 2024-12-13 10:30:15

我认为您的困惑是将结构与 Java 中的构造函数进行比较。 Java 中最接近的等价物是 class

class ATreeNode{
      int value;
      ATreeNode left;
      ATreeNode right;
}

正如其他答案所说,结构中的左节点和右节点是指针 - 很像(但不完全相同)Java 中的引用。

I think your confusion is comparing a struct to a constructor in Java. The closest equivalent in Java would be class:

class ATreeNode{
      int value;
      ATreeNode left;
      ATreeNode right;
}

As the other answers have said, the left and right node in the struct are pointers - much like (but not quite the same as) references from Java.

黯淡〆 2024-12-13 10:30:15

struct 不包含它自己。它包含两个指向其类型的指针。一个非常重要的区别。指针不是所指向的类型,而是可以在稍后将其取消引用为它们所指向的类型。

The struct doesn't contain it self. It contains two pointers to its type. A very important distinction. Pointers are not of the type the point to but can rather be dereferenced into what they point to at a later time.

清浅ˋ旧时光 2024-12-13 10:30:15

它不包含自身,它包含两个指向同一定义的指针。 leftPTR和rightPTR前面的*指向存储其他a_tree_node的内存位置。

It doesn't contain itself it contains two pointers to the same defenition. The * in front of the leftPTR and rightPTR point to memory location where other a_tree_node's are stored.

‖放下 2024-12-13 10:30:15

该结构体以形成链表的方式定义。在结构体内部定义两个指向结构体的指针。因此,该结构不包含其自身,而是包含两个指向结构的两个不同实例的指针。该指针甚至有可能是指向结构本身的指针。

The struct is defined in such a way that it forms a linked list. Inside the struct you define two pointers to structs. So, the struct does not contain itself, rather, it contains two pointers to two different instantiations of a struct. It is even possible the pointer is a pointer to the struct itself.

踏月而来 2024-12-13 10:30:15

当您来自 Java 时,您已经了解必要的概念,但缺乏 C 对数据和指针概念的严格要求。 leftPtr就像Java中的类类型(如Object)的变量,即它指向另一个对象,可能为Null,也可能指向另一个对象。

When coming from Java, you already know the necessary concepts, but lack the rigor C enforces on the concepts of data and pointers. leftPtr is just like a variable of class type (like Object) in Java, that is, it points to another object, might be Null or might point to another object.

ゃ人海孤独症 2024-12-13 10:30:15

它只是一个代表二叉树的 int 链表。

It's just a linked list of int representing a binary tree.

七七 2024-12-13 10:30:15

它包含类似结构的地址。
就像我们取一个树节点一样。

这意味着单个树节点还存储其他两个相似树节点的地址。

It contains the address of a simlar structure.
Like lets take a tree node.

it means that a single tree node also stores the address of two other similar tree nodes.

欲拥i 2024-12-13 10:30:15

问题中包含一个指向 struct a_tree_node 的指针。
指针类型的大小始终是常量,即 sizeof(unsigned integer)
因此在定义struct a_tree_node的大小时不会产生任何问题。
它不会是一个嵌套的struct...:) :)

Here in the question contains a pointer to struct a_tree_node.
The size of a pointer type is always constant i.e. sizeof(unsigned integer)
so it won't create any problem in defining the size of a struct a_tree_node.
It will not be a nested struct... :) :)

雅心素梦 2024-12-13 10:30:15
struct a_tree_node{int value;struct a_tree_node *leftPTR, *rightPTR; };

这段代码可以正常工作,因为我们引用的是结构体指针而不是其对象,因为指针的大小不是特定于数据类型的。这将取决于您的操作系统实际上有多少位,您的整数将占用多少字节
例如,在 gcc 上 sizeof(int) 是 4,所以 sizeof(leftPTR) 也相同
因此在运行时不会有递归 sizeof(a_tree_node)=12 (不考虑结构填充,因为它是编译器特定的)

struct a_tree_node{int value;struct a_tree_node left;};

此声明将导致错误,因为编译器将无法计算其大小
进入无限递归。

struct a_tree_node{int value;struct a_tree_node *leftPTR, *rightPTR; };

This code will work fine as we are referring pointer to structure not its object as size of pointer is not data type specific. It will depend on how much bit is your OS effectively your integer will take how much byte
e.g on gcc sizeof(int) is 4 so sizeof(leftPTR) is also same
so at run time there will be no recursion sizeof(a_tree_node)=12 (Not considering structure padding as it is compiler specific)

struct a_tree_node{int value;struct a_tree_node left;};

This declaration will leads to error as compiler wouldn't be able to compute its size
goes in infite recursion.

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