Chris Dargis
Chris Dargis

Reputation: 6053

interpreting c structs

Considering the following c code:

typedef struct ELE *tree_ptr

struct ELE {
   long val;
   tree_ptr left;
   tree_ptr right;
};

I believe ELE encapsulates a single node of a binary tree. Each node has some 32-bit value(val), a pointer to a left node, and a pointer to a right node.

Can someone confirm that my interpretation is correct?

Also, is ELE just an arbitrary variable name or some convention used to name a struct?

edit:

What if there was a c program that generated the following 3 lines of assembly code:

movq     %rbx, -24(%rsp)
movq     %rbp, -16(%rsp)
movq     %r12, -8(%rsp)

Are these 3 lines making room for the 3 elements of the struct on the stack?

Upvotes: 0

Views: 260

Answers (2)

fnl
fnl

Reputation: 2239

ELE is the so called tag of the struct. Tags reside in a separate namespace from other names in C. (other namespaces are those for members of respective structs and unions, labels and the rest (like variable names, function names, etc.))

So ELE (most likely for "Element") refers to this struct inside of the namespace for structs/unions/enums.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882068

That would most likely be ELEment, one element of the collection.

A few other points to consider:

  1. Your typedef needs a semicolon at the end.
  2. The C standard doesn't guarantee that long integers are 32 bits.
  3. The type tree_ptr indicates that it probably is a binary tree but all you know for sure is that it's a structure containing a long and the two pointers to the same type as the structure. It may well be a doubly-linked list if the coder that put it together was deranged or sadistic enough :-)

Upvotes: 2

Related Questions