3

I saw the following oddity in the XCB API header. Most types are defined with a type alias and a struct name in this way:

typedef struct xcb_window_iterator_t {
    xcb_window_t *data;
    int rem;
    int index;
} xcb_window_iterator_t;

But some typedefs omit the struct name.

typedef struct {
    unsigned int sequence;
} xcb_void_cookie_t;

What does this mean from the C point of view?

Is the latter a type with a name and a struct without a name? Or is it no type but only a struct with a name?

My confusion comes from the output of the gccxml.

For xcb_window_iterator_t it generates a Struct node:

$ xmllint --format --xpath '//Struct[@name="xcb_window_iterator_t"]' xcb.xml 
<Struct id="_199" name="xcb_window_iterator_t" context="_1" mangled="21xcb_window_iterator_t" demangled="xcb_window_iterator_t" location="f0:43" file="f0" line="43" artificial="1" size="128" align="64" members="_2240 _2241 _2242 _2243 _2244 _2245 _2246 " bases=""/>

and a Typedef node:

$ xmllint --format --xpath '//Typedef[@name="xcb_window_iterator_t"]' xcb.xml 
<Typedef id="_200" name="xcb_window_iterator_t" type="_199" context="_1" location="f0:47" file="f0" line="47"/>

But for xcb_void_cookie_t it generates just a Struct node:

$ xmllint --format --xpath '//Struct[@name="xcb_void_cookie_t"]' xcb.xml 
<Struct id="_967" name="xcb_void_cookie_t" context="_1" mangled="17xcb_void_cookie_t" demangled="xcb_void_cookie_t" location="f14:189" file="f14" line="189" size="32" align="32" members="_3655 _3656 _3657 _3658 _3659 " bases=""/>

But no Typedef node:

$ xmllint --format --xpath '//Typedef[@name="xcb_void_cookie_t"]' xcb.xml 
XPath set is empty

Does this mean xcb_void_cookie_t is no type but just a struct, although the code contains the typedef? Or is it a bug in gccxml?

ceving
  • 21,900
  • 13
  • 104
  • 178
  • You shouldn't bother at all with typedefs for structs. They are useless. They only save you typing "struct" here and there but obfuscate that something is a struct and needs to be operated on with `.` or `->`. – Jens May 24 '17 at 14:21
  • 2
    @Jens: that's one view — held by respected programmers. It is not the only view — other respected programmers prefer the alternative. – Jonathan Leffler May 24 '17 at 14:23

3 Answers3

4

The 1st snippet defines two types:

  • struct xcb_window_iterator_t
  • xcb_window_iterator_t

the latter one:

  • xcb_void_cookie_t

Is the lat[t]er a type with a name and a struct without a name

Yes, the typedef defines an "unnamed" (or "anonymous") structure

struct
{
  unsigned int sequence;
}

as the type xcb_void_cookie_t.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Then GCCXML is buggy, because it reports a named struct and no typedef? The right would be a struct with no name and a typedef with name. – ceving May 29 '17 at 08:43
1

If you have

typedef struct structname {
   ..
} typename;

you can write this to declare a variable a:

struct structname a;

or

typename a; 

If you have

typedef struct {
   ..
} typename;

you can only write this to declare a variable a:

typename a; 
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

The name after the struct keyword is optional. In the context of a typedef, that means an instance of the struct can only be declared with the given typedef.

In the case of the first typedef, you can declare a variable of this type in either of these ways:

  • struct xcb_window_iterator_t var
  • xcb_window_iterator_t var.

In the latter case, a variable of that type must be declared as:

  • xcb_void_cookie_t var
dbush
  • 205,898
  • 23
  • 218
  • 273