newb7777
newb7777

Reputation: 563

In C language, Structure, subtle difference in definition

I have seen format below for defining a structure

typedef struct Tag 
{
  type Member1;
  type Member2;
  <and so on..>
};

Some places I see the format below for defining a structure

typedef struct Tag 
{
  type Member1;
  type Member2;
  <and so on..>
} Tag;

and some places I see the format below for defining a structure

typedef struct 
{
  type Member1;
  type Member2;
  <and so on..>
} Tag;

If you notice the Tag syntax is after struct and also at the closing brace.

Questions.

1) What is the difference between the 3?

2) Is one preferred over the other or When are the best conditions you may want to use one over the other?

Upvotes: 3

Views: 1190

Answers (2)

Jay Buckman
Jay Buckman

Reputation: 578

The first example creates a definition for a struct named Tag. The second also creates a variable (which happens to be a Tag struct) named Tag. The third creates a variable named Tag with the specified structure - but the struct is never named so you can't use it again.

Which to use? Personally I'd use the first one and the separately declare any type Tag variables. If you are trying to eliminate a line of code, the second one works fine. If you are only going to use the struct once, you could use the third one.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311196

According to the C Standard (6.2.3 Name spaces of identifiers)

1 If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows:

— label names (disambiguated by the syntax of the label declaration and use);

— the tags of structures, unions, and enumerations (disambiguated by following any32) of the keywords struct, union, or enum);

— the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);

— all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

This is

struct Tag 
{
  type Tag;
  <and so on..>
};

a structure declaration with the name Tag and at the same time it is a structure definition because it not only introduces the type struct Tag but also declares its members.

You may at first declare a structure and then define it. For example

#include <stdio.h>

struct Tag;

struct Tag
{
    int Tag;
};

int main(void) 
{
    struct Tag Tag = { .Tag = 10 };

    printf( "Tag.Tag = %d\n", Tag.Tag );

    return 0;
}

The program output is

Tag.Tag = 10

In this declaration

struct Tag 
{
  type Tag;
  <and so on..>
} Tag;

there is declared an object of the type struct Tag with the identifier Tag. The structure has a data member with name Tag. Thus there are declared two entities: a type struct Tag and an object with the name Tag of the structure type.

In this declaration

struct 
{
  type Tag;
  <and so on..>
} Tag;

there is declared an object of unnamed structure type with the identifier Tag. The problem with this declaration is that you can not refer to the type of the object.

You can meet one more declaration like

typedef struct Tag 
{
  type Tag;
  <and so on..>
} Tag;

In this typedef declaration the identifier Tag serves as an alias of the type struct Tag.

Or

typedef struct  
{
  type Tag;
  <and so on..>
} Tag;

In this typedef declaration the identifier Tag serves as an alias of the unnamed structure type.

The C 2011 allows to declare anonymous structures. From the C Standard (6.7.2.1 Structure and union specifiers)

13 An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. This applies recursively if the containing structure or union is also anonymous.

Upvotes: 4

Related Questions