Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable name after struct definition [duplicate]

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;
like image 816
heretoinfinity Avatar asked Jul 22 '26 05:07

heretoinfinity


2 Answers

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.

like image 190
Eric Postpischil Avatar answered Jul 24 '26 18:07

Eric Postpischil


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;
like image 31
anastaciu Avatar answered Jul 24 '26 20:07

anastaciu