5

What are bar and x below and why are they located there? Are they instances of the structs?

I was reading this Stackoverflow question whose code I have pasted here and I found that the poster and one of the answers use variable names [bar and x] after the definition of the struct. cpp reference doesn't use this syntax their examples and I don't know what the syntax is called nor what the syntax does to look it up.

If it is just about creating an instance of a struct, why not write it in the next line like cpp reference does in its examples?

struct _bar_ 
    {
        int a;
    } bar; 
struct test {
      int value;
   } x;
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33

4 Answers4

6

struct test { int value; } x; declares x to be an object of type struct test. It is a simple declaration, the same as int x, with struct test { int value; } in place of int.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 2
    why do it that way though? Isn't it hard to see the `bar`? Why not write it on a new line? Is this a one-off thing? – heretoinfinity Feb 09 '20 at 16:37
  • 3
    If by “one-off” thing, you are asking whether `struct test` can be used again later in the same scope to declare additional objects, then, no, it is not a one-off thing; it may be used again as `struct test` or (in C++ but not C) `test`. If the tag were omitted, the structure would be a unique definition of a type without a name. As for “why”, people learn to read code. Additionally, the identifiers for the declared objects do appear on a new line, as `} bar;` and `} x;` appear on lines of their own. This becomes quite recognizable after a little experience. – Eric Postpischil Feb 09 '20 at 16:43
2

In

struct _bar_ {
    int a;
} bar; 

bar is a declaration of a variable of type struct _bar_.

It's the same as:

struct _bar_ bar;
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • why do it that way though? Isn't it hard to see the `bar`? Why not write it on a new line? Is this a one-off thing? – heretoinfinity Feb 09 '20 at 16:37
  • I guess it's a matter of style, both are the same, you can use the created variable `bar` in the same way for both declaration cases. – anastaciu Feb 09 '20 at 16:52
1

By using this definition:

struct _bar_ 
    {
        int a;
    } bar; 

Here you've just declared a variable of type struct _bar_.

struct product {
  int weight;
  double price;
} ;

product apple;
product banana, melon;

In this case, where object_names are specified, the type name (product) becomes optional: struct requires either a type_name or at least one name in object_names, but not necessarily both.

From: Data structures

some user
  • 1,693
  • 2
  • 14
  • 31
1

As all other answers have stated, defining a name after a struct definition creates an instance of that struct with that name in whatever scope it was defined. So, struct __bar__ { ... } bar; is equivalent to struct __bar__ { ... }; __bar__ bar;. As of writing, however, none of the other answers attempt to answer the second question: "Why not write it in the next line like cpp reference does in its examples?"

The most practical argument I can think of for using this "name after" syntax is when you only need one instance of this struct. This way, it is guaranteed that both the definition and declaration happen in the same place; which is not guaranteed using the "next line" approach. This also has the added benefit of only having one place where the struct is defined, which means if you want to change it at any point you only have to go to 1 spot instead of 2 (potentially disjunct) ones.

This translates well for when you (absolutely) need a single set of data in a global scope:

// 1 bar, in the global scope
struct Bar {
    int a;
} global_bar;

int main()
{
    // Do something with global bar
    global_bar.a = 100;

    return 0;
}

Whereas, if you wanted multiples of the struct you would probably tend towards code like:

struct Bar {
    int a;
};

int main()
{
    // 2 bars, in the local scope
    Bar bar_1 = Bar();
    Bar bar_2 = Bar();

    // Do something with the local bars...

    return 0;
}
RedRuin
  • 122
  • 7