Re: struct & enum & :: visibility operator
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 21 Jul 2008 18:27:40 -0500 (CDT)
"robbio" <robbio72@xxxxxxxxx> writes:
Hi at all,
I have a header file like this:
-------- file.h ------------
....
extern "C"
extern "C" is a syntax error in C. Apparently this header is meant to
be C++, which is a different language with its own newsgroups.
It's common to use something like this:
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#ifdef __cplusplus
}
#endif
if you really feel the need to use the same header for both C and C++.
{
...
typedef _S S;
This attempts to declare "S" as an alias for the type "_S". You
haven't declared a type called "_S", so of course the compiler doesn't
know what you're talking about. Things don't become visible until
*after* you declare them.
Well, mostly. You *could* have declared
typedef struct _S S;
This introduces "struct _S" as an incomplete type; you can't declare
an object of that type, but you can declare a typedef or a pointer
type that uses it.
...
struct _S
And, in C, this doesn't declare a type "_S"; it declares a type
"struct _S". Unlike in C++, you can't refer to "struct _S" as just
"_S".
Also, it's dangerous to use identifiers starting with an underscore;
such identifiers are reserved to the implementation. The rule is
actually a little more complicated, but that's all you need to
remember: don't declare things using identifiers starting with
underscores.
You don't really *need* the typedef at all. You can just declare your
type as:
struct S { /* ... */ };
and refer to it as "struct S". Or, if you prefer to have a one-word
name for the type, you can use the same identifier for the typedef as
for the struct tag:
typedef struct S { /* ... */ } S;
If you need to refer to the type name inside the declaration, things
are a little more complicated; I'll leave the details for later.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- References:
- struct & enum & :: visibility operator
- From: robbio
- struct & enum & :: visibility operator
- Prev by Date: Re: how to generate random char in C programming?
- Next by Date: Re: how to generate random char in C programming?
- Previous by thread: struct & enum & :: visibility operator
- Next by thread: Re: struct & enum & :: visibility operator
- Index(es):
Relevant Pages
|