开发者

Understanding the flow of a struct in C

开发者 https://www.devze.com 2023-04-08 20:46 出处:网络
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.

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 h开发者_JS百科ow this works, I am a little confused because this struct contains itself.


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.


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.


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.


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.


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.


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.


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


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.


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... :) :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号