different struct

J

Jim Johnson

what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;

================================
struct {

.... etc

} SetupRecord;
 
I

Ian Collins

Jim said:
what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;
This defines a type in C.
================================
struct {

.... etc

} SetupRecord;

this does not.

Only use the first form if you are sharing types between C and C++.

The second form is unusual, use

struct SetupRecord {...};
 
J

Jack Klein

So there are 2 ways to declare a struct

1) C way of declaring

No, this is not declaring anything, this is defining a type, and also
creating an alias for it.

This is also perfectly valid C++. It defines a structure type with no
name, and defines SetupRecord as an alias for that type. This is
actually not a particularly good idea in either language.
2) C++ way of declaring a struct

Again, your sample below does not declare a struct, it defines a
struct type.

This is perfectly valid C does as well. In both languages, it defines
a structure type named SetupRecord. In either language, the type can
be referred to by the two-word phrase "struct SetupRecord". In C++
only, it can also be referred to merely as SetupRecord.
may I correct?

and also the following is incorrect synatx to declare a struct

Your terminology is still incorrect. Your snippet below defines a
structure type, and also defines an object of that structure type.

You have defined an unnamed structure type, and an instance of that
type named SetupRecord. In this situation, SetupRecord is the name of
a single object of this type, and not of the structure type itself.

It is perfectly valid code, and this object can be accessed anywhere
within its scope. Compile and execute the following example:

#include <iostream>

struct { int x; int y; } ss;

void show_me()
{
std::cout << ss.x << " + " << ss.y << " = "
<< (ss.x + ss.y) << std::endl;
}

int main()
{
ss.x = 2;
ss.y = 3;
show_me();
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
J

Jim Johnson

So there are 2 ways to declare a struct

1) C way of declaring
typedef struct {

.... etc

} SetupRecord;

2) C++ way of declaring a struct
struct SetupRecord {

.... etc

} ;

may I correct?

and also the following is incorrect synatx to declare a struct
 
J

James Kanze

This defines a type in C.

And in C++. "If the typedef declaration defines an unnamed class
(or enum), the first typedef-name declared by the declaration to
be that class type (or enum type) is used to denote the class
type (or enum type) for linkage purposes only." The result is
sort of similar to:

struct SetupRecord { ...etc... } ;

This is really more of a hack for reasons of C compatibility
that anything else, and I've never seen it used in pure C++
code. (But it does occur in headers that are designed to be
used by both C and C++.) As you say later, prefer the usual C++
convention.
this does not.

It defines an unnamed type. Note that unlike C, C++ uses name
equivalent for typing. Thus, in C:

struct { int i ; } a ;
struct { int i ; } b ;

a and b have the same type (and an expression like "a = b" is
legal); in C++, they don't (and "a = b" should provoke a
compiler error).

I tend to avoid unnamed types in C++, even in the cases where
their use poses no problems.
 
J

James Kanze

No, this is not declaring anything, this is defining a type,
and also creating an alias for it.

In C. In C++, the issue is a bit more complex.
This is also perfectly valid C++. It defines a structure type
with no name, and defines SetupRecord as an alias for that
type.

Actually, it defines a class type with the name SetupRecord, and
also defines SetupRecord as an alias for that type. (But this
is more a compatibility hack than anything else.)

There is a fundamental difference between C and C++ here. In C,
structure names are NOT type names; in C++, they are. And C
types by structure (although if you give the struct a name, it is
part of the structure, and thus the type), C++ by name. For
example:

typedef struct { int i ; } A ;
typedef struct { int i ; } B ;

In C, you have one type ("struct {int i;}"), with two names (or
aliases to the type). In C++, you have two different types, A
and B (and A and B are also aliases for the types).
This is actually not a particularly good idea in either
language.

Opinions vary in C. I tended not to use it when I was working
in C, but a lot of programmers do.
Again, your sample below does not declare a struct, it defines a
struct type.

You mean a class type, I suppose. In C++, there aren't any
struct types.
This is perfectly valid C does as well. In both languages, it defines
a structure type named SetupRecord.

Not really. In C, it defines a structure type with the tag
SetupRecord. In C++, it defines a class type with the type name
SetupRecord.
In either language, the type can be referred to by the
two-word phrase "struct SetupRecord".

The practical effects are the same, but the actual definition of
the phrase is very different in the two languages.
In C++ only, it can also be referred to merely as SetupRecord.

That's because in C++, SetupRecord is a type name; in C, it's
not.

Fundamentally, the two languages are very different in this
regard. Practically, C++ has a couple of special case hacks so
that most C uses end up still being legal, and having (almost)
the same effect.
 
M

Michael.Boehnisch

Jim said:
what's the difference when declare a struct with typedef or NO
typedef?
[..]
this does not.
Only use the first form if you are sharing types between C and C++.
The second form is unusual, use
struct SetupRecord {...};

Both versions are valid C and C++ code. Unnamed structs and named
structs are accepted in both languages.

typedef struct { int i; } T;
defines a new datatype "T". You can create objects of type "T":
T x; x.i = 42;

struct { int i; } x;
defines a new object of type "unnamed struct with int data member i".
You can use it as in
x.i = 42;

struct Name { int i; }
defines a new datatype "struct Name". You can create objects of this
type:
struct Name x; x.i = 42;
In C++ you may leave out the keyword struct:
Name x;
is a valid definition in C++, but not in C. "struct" and "class"
keywords differ in one detail only: struct members are "public" by
default, in a class they are "private".

struct Name { int i; } x;
is a combination of the two immediately above. It defines a new
datatype and immediately declares a variable of this type.
struct Name y; y.i = 35; x.i = 42;


Note, "struct Name" and "T" are not the same types.

best,

Michael.
 
T

Tim Slattery

Jim Johnson said:
what's the difference when declare a struct with typedef or NO
typedef?

================================
typedef struct {

.... etc

} SetupRecord;

This defines SetupRecord to be a type, which corresponds to this
structure. You can define other items of type SetupRecord:

SetupRecord xyz;

"xyz" is now a variable of type SetupRecord.
================================
struct {

.... etc

} SetupRecord;

This defines a variable named SetupRecord. It's a structure whose
properties you've just defined.
 
R

Ron Natalie

Ian said:
This defines a type in C.


this does not.

Only use the first form if you are sharing types between C and C++.

The second form is unusual, use

struct SetupRecord {...};
The latter doesn't define a type at all, it defines a variable of
unnamed type called SetupRecord.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top