5

I have read a article in the following link

http://www.embedded.com/electronics-blogs/programming-pointers/4024450/Tag-vs-Type-Names

Here author says that, use of follwing is wrong.

struct s
{
--
};

s var;

But in my sample code its works perfectly.

  1 #include<iostream>
  2 using namespace std;
  3
  4 struct s
  5 {
  6    int sd;
  7 };
  8 s v;
  9
 10
 11
 12 int main()
 13 {
 14
 15    v.sd=10;
 16    cout<<v.sd;
 17    return 0;
 18 }

EDIT:

What the actual difference? why it works in c++ and not works in c;

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38

2 Answers2

11

It is the difference between C++ and C. The author you are citing speaks about C while you use C++ code instead of C code. In C you have to specify keyword struct, union or enum before declaring variables of correspondings types.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Also I would like to append that names of tags in C form their own namespace. So the same name can be used with a structure tag and with a variable. – Vlad from Moscow Jan 06 '14 at 14:57
  • is tags in c++ are considered as normal identifier... Typically in c its not am i correct @Vlad – VINOTH ENERGETIC Jan 06 '14 at 15:00
  • In C++ structures, unions and enums name considered together with names of variables. If for example a name of a structure coinside with a name of a variable then the name of the variable (or function) hides the name of the structure.In this case you should use an elaborated structure name, For example struct A {}; A A; /* it is a correct definition */ A = ( struct A )(); /* using of the elaborated name */ – Vlad from Moscow Jan 06 '14 at 15:03
1

The article says that a user-defined type using

struct s {
   ...
};

defines a tag name s. To name the actual type, you can write struct s (C or C++) or class s (C++ only), but C++ makes both keywords optional (and almost never actually used). So whenever you write s in C++ it's actually interpreted as the correct type, while in C the keyword is obligatory to name the type.

So long story short: in C++ there is no difference in writing struct s, class s or s, they all mean the same.

In order to define the type name s in a C/C++ shared header, one typically writes

typedef struct {
   ...
} s;

Then the type s can be used in both C and C++ without the need to write struct.

leemes
  • 44,967
  • 21
  • 135
  • 183
  • http://coliru.stacked-crooked.com/a/b071707eb46e1a31 In fact, 7.1.3/5 explicitly states that what you say is invalid C++, is valid. 7.1.3/3 explicitly states that the other one is valid too :) – Lightness Races in Orbit Jan 06 '14 at 18:05
  • Oh... Then I remember something wrong. I once tried to define a struct in a C++/CL shared header (not C), and in CL something didn't work which normally works in C/C++ shared headers. My workaround was to explicitly give the struct and the typedef different names. Thanks for pointing this out; I'll remove the part from my answer. – leemes Jan 06 '14 at 18:34