1

Regarding binary trees - I had seen the following code as part of the solution for a problem:

struct Node
{
    int key;
    struct Node *left, *right;
};

My question is what does "struct Node *left, *right" mean given it is defined w/in the body of the first struct Node definition. Also if this is C++ why would you use struct instead of just class/object here?

9
  • 1
    struct and class are essentially the same thing in C++. The only difference is that struct members are public by default and class members are private by default. For that reason it is common to use class anytime you want encapsulation, and struct when you merely want to gather together related public data members e.g. std::pair. Commented Apr 11, 2015 at 5:48
  • 1
    This has been answered before: stackoverflow.com/a/7729819/1236397 Commented Apr 11, 2015 at 5:50
  • 2
    @djfkdjfkd39939: You can, as long as you're writing C++ and not C. Commented Apr 11, 2015 at 5:54
  • 1
    @djfkdjfkd39939 with pointers in the background. In JS, pointers are not exposed in the language, but object have reference semantics meaning that a practical implementation will use pointers in the background. (JavaScript engines are almost universally written in C or C++.) Commented Apr 11, 2015 at 6:06
  • 1
    @djfkdjfkd39939 as to your question "why do we need the struct keyword in C but not in C++": it's a purely syntactic thing. C++'s grammar allows the omission of struct, C's doesn't. It has nothing to do with how "advanced" each language is. (C is just as "advanced" as C++. It takes a lifetime to master. It's just not as complex as C++.) Commented Apr 11, 2015 at 6:08

3 Answers 3

2

In C++ (and especially C), types are usually always represented by their construct.

For instance,

enum Foo { ... };
void doSomethingWithAFoo(enum Foo f);

struct Bar { ... };
void doSomethingWithABar(struct Bar bar);

While this is required in C, it is not in C++. In C, it is achieved by using a typedef.

typedef struct { ... } Foo; // Can now be referenced with just `Foo`

However, there is a particular part of the spec that states that struct types cannot have instance of themselves inside of them (more specifically it states types cannot refer to themselves before they're fully declared).

Except in pointer form. This is because pointers are known sizes at the beginning of compilation, whereas structures are only known after they are declared.


Since structs pre-date C++ (only by a little) and have been present since ANSI C (C89) and before in most major compilers, they are also present in C++ (since ANSI C can be compiled gracefully in compliant C++ compilers).

However, C++ adds the concept of classes, which don't exist in C. As others have mentioned, classes and structs are similar in that they both holds members. In C++, structs can have methods just like classes - obviously this is not the case in C.

The only difference as far as I'm aware is the visibility; structs default to public and classes default to private. C does not have the notion of visibility.

Sign up to request clarification or add additional context in comments.

8 Comments

I explain that; you're more than likely working with code that is actually C. It just happens to work with your C++ compiler.
Interesting, so it is in pointer form bc it is of type struct and its recursive (i.e. "struct Node *left, *right" instead of "struct Node left, right")?
Correct. Think about it; if the struct had to allocate space for itself, and then a copy of itself, those copies of themselves would also have to allocate space for more copies, and so on. It'd be never ending.
@djfkdjfkd39939: If you wrote Node left, right; (without the pointers), then each Node would contain two Nodes. How much memory, then, would a Node require?
C structs have existed since the 1970s - with K&R C. It is not just "certain compilers" that supported them.
|
1

Means exactly what it looks like. A recursive declaration just means a Node struct had two fields that are pointers to other Nodes.

I've read that the main difference between structs and classes in C++ is the default permissions (structs are all public by default). Slightly simpler, especially since inheriting is unlikely.

1 Comment

Why couldn't you just write "Node *left, *right" instead of "struct Node *left, *right"?
0

The meaning of struct Node *left, *right; within the definition of struct Node is that each instance of struct Node contains members (left and right) that point to other struct Nodes.

It is the programmer's responsibility to ensure that, whenever a struct Node is created, that those members are initialised appropriately. They may be set to NULL (indicating they don't point at any object) or to the address of another struct Node.

In C++, initialisation of left and right will often be done in a constructor. C doesn't have that feature so, every time some C code creates a struct Node, it will typically have to explicitly initialise the left and right members.

The code you have shown is really C, although it will be accepted by a C++ compiler. C++ allows the declaration struct Node *left, *right; to omit the struct keyword. C does not.

In C++, a struct and a class are the same thing - the difference is accessibility of members: C++ struct members are public and class members are private by default. C++ structs (and classes) are able to do numerous other things that C structs cannot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.