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;

10
  • 3
    This works in C++, but not in C Commented Jan 6, 2014 at 14:52
  • Did you miss a semi-colum in the above example by mistake? Commented Jan 6, 2014 at 14:52
  • No i have used in my code Commented Jan 6, 2014 at 14:53
  • 2
    Pick a language. The author seems to be talking about C. Commented Jan 6, 2014 at 14:53
  • Also have a look at stackoverflow.com/questions/252780/… Commented Jan 6, 2014 at 14:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
is tags in c++ are considered as normal identifier... Typically in c its not am i correct @Vlad
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 */
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.

2 Comments

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 :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.