0

I am relatively new to C and I came across a typedef for a structure with no name.

What is the point of it?

#define POLY(name,deg)    \
    term name[deg] = {0};

typedef struct {
  int coeff;
  int exp;
} term;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256

1 Answers1

1

term alone can be used to represent the type then. In language C, with struct term { ... };, you will have to use struct term to refer to the type.

This is different from C++, where a definition like struct term { ... }; implicitly introduces both struct term and term alone as valid references.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • So it's shorthand for struct term? – Brian T Brennglass Jan 07 '19 at 22:15
  • 1
    It's shorthand for your anonymous struct – dan.m was user2321368 Jan 07 '19 at 22:16
  • most of the structs i see have tags with an underscore prepended, so it would look like "typedef struct _term { int cooeff; int exp;} term;" – Bwebb Jan 07 '19 at 22:18
  • @BrianTBrennglass `term` is a type *alias*. It aliases the type immediately following the `typedef` keyword (that's how all type aliases work in C). In this case, that is an anonymous (untagged) compound type. As an anonymous type, you cannot `struct name something;` because there is no such name. Because of the alias, however, you can `term something;` and the meaning is clear. – WhozCraig Jan 07 '19 at 22:19
  • yes and no; yes: any `typedef` introduces an alias and is then a "shorthand", if you like; no: as you introduce an anonymous struct, which has no "name", you would have no chance to refer to the struct otherwise, such that there is nothing for which it could be a shorthand; it's the only possibility then. – Stephan Lechner Jan 07 '19 at 22:19
  • 2
    @Bwebb FIY, prepending a type (or identifier or a macro name) with underscore is undefined behavior according to C standard. Names with leading underscore are reserved for implementation. So please, don't do it. – Eugene Sh. Jan 07 '19 at 22:22
  • I saw this comment in another thread. fyi, page 166 of C99 draft, All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. And All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces. – Bwebb Jan 07 '19 at 22:24
  • @EugeneSh. Ive been looking into this, and why i do it and why I've seen it in professional code. It seems like its a MISRA(ble) thing: Rule 5.6 (advisory): No identifier in one name space should have the same spelling as an identifier in another name space, with the exception of structure member and union member names. [MISRA C 2004] – Bwebb Jan 07 '19 at 22:46
  • @Bwebb I am working in a very professional environment and seeing it all over the place :) But we have recently employed the SEI CERT standard and now having much troubles with this stuff. Here is the page helping interpret this rather vague standard wording: http://c-faq.com/decl/namespace.html – Eugene Sh. Jan 07 '19 at 22:52
  • @EugeneSh Can you help me understand what you mean by "Names with leading underscore are reserved for implementation"? Does this mean i shouldnt write libraries with _structname? Does this mean if im writing embedded code that isnt a library I dont have to worry? Thanks in advance. *EDIT* looking to SEI CERT – Bwebb Jan 07 '19 at 22:52
  • Bwebb "Does this mean if im writing embedded code that isnt a library I dont have to worry?". Yes, remain worried. As @EugeneSh. said [here](https://stackoverflow.com/questions/54082560/what-is-the-point-of-creating-a-typedef-of-an-anonymous-structure#comment94998828_54082582_), "don't do it". "And All identifiers..."" is from the C spec. – chux - Reinstate Monica Jan 07 '19 at 23:10