In article <11*********************@p79g2000cwp.googlegroups. com>
werasm <w_*****@telkomsa.netwrote:
>struct ms{ };
creates a structure identified by "ms" instantiated as follow:
struct ms inst;
Yes (although there must be contents within the {}s, in C; C++ differs
here).
>typedef struct ms{ } ms;
creates a named type "ms" instantiated as follows:
ms inst;
...but can also be identified by "ms" in
struct ms inst;
Essentially. However, note that it is the "struct" keyword that
creates the type, which is named "struct ms". The "typedef",
whose syntax includes things like:
typedef existing_type alias_1, alias_2, alias_3;
then creates however many aliases you list for the existing type.
The syntax for "typedef" is identical to that for ordinary variable
declarations, so:
typedef int tA, tB[10], *tC, tD(void);
defines four aliases: tA means "int", tB means "int [10]",
tC means "int *", and tD means "int (void)". This last one is
a function type, and can be used to declare functions but not
to define them:
tD foo; /* equivalent to int foo(void); */
tD foo { return 42; } /* WRONG; diagnostic required */
>typedef struct { } ms;
creates an anonymous struct that can be instantiated as follows:
ms inst;
Yes: as before, the "struct" creates the type, then the subsequent
"typedef" gives it an alias.
>BTW, is this instantiable in many scopes?:
void foo1(void){ ms inst; }
void foo2(void){ ms inst; }
Yes. Typedef-ed identifiers obey normal scoping rules.
>typedef struct ms{ };
... is this the same as ...
struct ms{ };
It is analagous to:
int ;
which requires a diagnostic.
>The reason behind my questions (I'm a C++ programmer, BTW), is that we
want to share some C code. I therefore want to create the equivalent of
a C++ structure in C (apart from the structure not having member
functions).
There are a number of subtle differences here between C and C++,
but if you want your C type-names to "work like" C++'s, do:
/* optional: struct ms; */
typedef struct ms ms;
struct ms {
... contents ...
};
The initial "typedef" contains the "struct ms" part, which --
assuming "struct ms" has not yet been defined elsewhere -- creates
the type as an "incomplete type". The "typedef" part then gives
it the alias. Having created both the incomplete type *and* the
alias, you can now go on to complete the type, *and* use the alias:
typedef struct ms ms;
struct ms {
ms *next;
... additional elements ...
};
See also <http://web.torek.net/torek/c/types2.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.