Hi at all,

I have a header file like this:

-------- file.h ------------
...
extern "C"
{
...
typedef struct _S S;
...

struct _S
{
...
enum
{
a,
b
}myenum;
...
};
}
--------- end of file.h -------------


and a C file like this

----------- file.c ----------------

....
#include "file.h"


static void myFunc(S* pS)
{
....
if (... == _S::a)
....
....
}

-----------end of file.c --------------

I'm using the Microsoft 2005 environment and so its compiler. The compiler
says me that there is an error in myFunc, the error is that _S is an
undeclared indentifier. I'm not an expert of C but I don't know where the
problem is.

Can anyone help me, please?

thanks a lot
roberto

Re: struct & enum & :: visibility operator by David

David
Thu Jul 17 13:58:41 CDT 2008

robbio wrote:
> Hi at all,
>
> I have a header file like this:
>
> -------- file.h ------------
> ...
> extern "C"
> {
> ...
> typedef struct _S S;
> ...
>
> struct _S
> {
> ...
> enum
> {
> a,
> b
> }myenum;
> ...
> };
> }
> --------- end of file.h -------------
>
>
> and a C file like this
>
> ----------- file.c ----------------
>
> ....
> #include "file.h"
>
>
> static void myFunc(S* pS)
> {
> ....
> if (... == _S::a)
> ....
> ....
> }
>
> -----------end of file.c --------------
>
> I'm using the Microsoft 2005 environment and so its compiler. The compiler
> says me that there is an error in myFunc, the error is that _S is an
> undeclared indentifier. I'm not an expert of C but I don't know where the
> problem is.
>
> Can anyone help me, please?


Roberto:

Shouldn't that be

S::a

or

struct _S::a

? Your code would have been OK in C++ (which does not need the typedef trick).

--
David Wilkinson
Visual C++ MVP

Re: struct & enum & :: visibility operator by robbio

robbio
Thu Jul 17 14:34:58 CDT 2008


"David Wilkinson" <no-reply@effisols.com> wrote in message
news:OFKrg8D6IHA.4560@TK2MSFTNGP04.phx.gbl...
> robbio wrote:
>> Hi at all,
>>
>> I have a header file like this:
>>
>> -------- file.h ------------
>> ...
>> extern "C"
>> {
>> ...
>> typedef struct _S S;
>> ...
>>
>> struct _S
>> {
>> ...
>> enum
>> {
>> a,
>> b
>> }myenum;
>> ...
>> };
>> }
>> --------- end of file.h -------------
>>
>>
>> and a C file like this
>>
>> ----------- file.c ----------------
>>
>> ....
>> #include "file.h"
>>
>>
>> static void myFunc(S* pS)
>> {
>> ....
>> if (... == _S::a)
>> ....
>> ....
>> }
>>
>> -----------end of file.c --------------
>>
>> I'm using the Microsoft 2005 environment and so its compiler. The
>> compiler
>> says me that there is an error in myFunc, the error is that _S is an
>> undeclared indentifier. I'm not an expert of C but I don't know where the
>> problem is.
>>
>> Can anyone help me, please?
>
>
> Roberto:
>
> Shouldn't that be
>
> S::a
>
> or
>
> struct _S::a
>
> ? Your code would have been OK in C++ (which does not need the typedef
> trick).
>
> --
> David Wilkinson
> Visual C++ MVP

sorry, I don't understand what you told me. I know that in C++ all work
well, I tried it.

Either S::a or _S::a are wrong?
in this case: if I want to keep the visibility of a, b variable as
enumerator inside my _S struct how I could do?
does C not have the operator :: to use in that way?

thanks
roberto



Re: struct & enum & :: visibility operator by Joe

Joe
Thu Jul 17 15:03:54 CDT 2008

"robbio" <roberto674@supereva.it> wrote in
news:#lSX1QE6IHA.300@TK2MSFTNGP05.phx.gbl:

>
> "David Wilkinson" <no-reply@effisols.com> wrote in message
> news:OFKrg8D6IHA.4560@TK2MSFTNGP04.phx.gbl...
>> robbio wrote:
>>> Hi at all,
>>>
>>> I have a header file like this:
>>>
>>> -------- file.h ------------
>>> ...
>>> extern "C"
>>> {
>>> ...
>>> typedef struct _S S;
>>> ...
>>>
>>> struct _S
>>> {
>>> ...
>>> enum
>>> {
>>> a,
>>> b
>>> }myenum;
>>> ...
>>> };
>>> }

The idiomatic way of doing this in C is:

typedef struct _S
{
enum
{
a,
b
} myenum;
....
} S;


The original problem was that the C compiler didn't know what an _S was
for the typedef. If I recall correctly, you can declare an alias for a
forward declared type, though if you wanted to do an experiment you
could try prefacing the typedef with a 'struct _S;'. If you can, I
would just use the form I wrote above.

HTH,
joe

Re: struct & enum & :: visibility operator by robbio

robbio
Thu Jul 17 15:44:46 CDT 2008


"Joe Greer" <jgreer@doubletake.com> wrote in message
news:Xns9ADEA36C49E39jgreerdoubletakecom@85.214.90.236...
> "robbio" <roberto674@supereva.it> wrote in
> news:#lSX1QE6IHA.300@TK2MSFTNGP05.phx.gbl:
>
>>
>> "David Wilkinson" <no-reply@effisols.com> wrote in message
>> news:OFKrg8D6IHA.4560@TK2MSFTNGP04.phx.gbl...
>>> robbio wrote:
>>>> Hi at all,
>>>>
>>>> I have a header file like this:
>>>>
>>>> -------- file.h ------------
>>>> ...
>>>> extern "C"
>>>> {
>>>> ...
>>>> typedef struct _S S;
>>>> ...
>>>>
>>>> struct _S
>>>> {
>>>> ...
>>>> enum
>>>> {
>>>> a,
>>>> b
>>>> }myenum;
>>>> ...
>>>> };
>>>> }
>
> The idiomatic way of doing this in C is:
>
> typedef struct _S
> {
> enum
> {
> a,
> b
> } myenum;
> ....
> } S;
>
>
> The original problem was that the C compiler didn't know what an _S was
> for the typedef. If I recall correctly, you can declare an alias for a
> forward declared type, though if you wanted to do an experiment you
> could try prefacing the typedef with a 'struct _S;'. If you can, I
> would just use the form I wrote above.
>
> HTH,
> joe

Hi Joe,
I know the iodiomatic way you are saying. I tried your example I got the
same problem. Try you it.
I tried also to to a forward declarations, that if I understand is:

after:
----------------
struct _S
{
...
};
-----------

I put the following line:

typedef struct _S _S;

in that case the compiler says "illegal use of this type (_S) as an
expression" on the "if" line in the file.c file.

Any idea?

thanks
roberto




Re: struct & enum & :: visibility operator by David

David
Thu Jul 17 15:44:05 CDT 2008

robbio wrote:
> "David Wilkinson" <no-reply@effisols.com> wrote in message
> news:OFKrg8D6IHA.4560@TK2MSFTNGP04.phx.gbl...
>> robbio wrote:
>>> Hi at all,
>>>
>>> I have a header file like this:
>>>
>>> -------- file.h ------------
>>> ...
>>> extern "C"
>>> {
>>> ...
>>> typedef struct _S S;
>>> ...
>>>
>>> struct _S
>>> {
>>> ...
>>> enum
>>> {
>>> a,
>>> b
>>> }myenum;
>>> ...
>>> };
>>> }
>>> --------- end of file.h -------------
>>>
>>>
>>> and a C file like this
>>>
>>> ----------- file.c ----------------
>>>
>>> ....
>>> #include "file.h"
>>>
>>>
>>> static void myFunc(S* pS)
>>> {
>>> ....
>>> if (... == _S::a)
>>> ....
>>> ....
>>> }
>>>
>>> -----------end of file.c --------------
>>>
>>> I'm using the Microsoft 2005 environment and so its compiler. The
>>> compiler
>>> says me that there is an error in myFunc, the error is that _S is an
>>> undeclared indentifier. I'm not an expert of C but I don't know where the
>>> problem is.
>>>
>>> Can anyone help me, please?
>>
>> Roberto:
>>
>> Shouldn't that be
>>
>> S::a
>>
>> or
>>
>> struct _S::a
>>
>> ? Your code would have been OK in C++ (which does not need the typedef
>> trick).
>>
>> --
>> David Wilkinson
>> Visual C++ MVP
>
> sorry, I don't understand what you told me. I know that in C++ all work
> well, I tried it.
>
> Either S::a or _S::a are wrong?
> in this case: if I want to keep the visibility of a, b variable as
> enumerator inside my _S struct how I could do?
> does C not have the operator :: to use in that way?

roberto:

I'm not an expert in C either. My original point was that the C compiler does
not understand the symbol S_ alone (without the word struct), so you should use
the typedef'd name S, or the full name struct S_.

But a little research reveals that C does not have the scope resolution operator
::. Have you tried without the scope resolution?

Are you sure it is legal C to declare an enum inside a struct definition? Why
not just declare the struct outside the struct? (Different names for the enum
items might be a good idea; I always use caps for enum items.)

--
David Wilkinson
Visual C++ MVP

Re: struct & enum & :: visibility operator by Igor

Igor
Thu Jul 17 15:53:51 CDT 2008

robbio <roberto674@supereva.it> wrote:
> I have a header file like this:
>
> -------- file.h ------------
> ...
> extern "C"
> {
> ...
> typedef struct _S S;
> ...
>
> struct _S
> {
> ...
> enum
> {
> a,
> b
> }myenum;
> ...
> };
> }
> --------- end of file.h -------------
>
>
> and a C file like this
>
> ----------- file.c ----------------
>
> ....
> #include "file.h"
>
>
> static void myFunc(S* pS)
> {
> ....
> if (... == _S::a)
> ....
> ....
> }

There ain't no such thing as a scope resolution operator (::) in C. Just
write "a" instead of _S::a
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: struct & enum & :: visibility operator by robbio

robbio
Thu Jul 17 15:57:15 CDT 2008


"David Wilkinson" <no-reply@effisols.com> wrote in message
news:%23VqMa3E6IHA.3684@TK2MSFTNGP05.phx.gbl...
> robbio wrote:
>> "David Wilkinson" <no-reply@effisols.com> wrote in message
>> news:OFKrg8D6IHA.4560@TK2MSFTNGP04.phx.gbl...
>>> robbio wrote:
>>>> Hi at all,
>>>>
>>>> I have a header file like this:
>>>>
>>>> -------- file.h ------------
>>>> ...
>>>> extern "C"
>>>> {
>>>> ...
>>>> typedef struct _S S;
>>>> ...
>>>>
>>>> struct _S
>>>> {
>>>> ...
>>>> enum
>>>> {
>>>> a,
>>>> b
>>>> }myenum;
>>>> ...
>>>> };
>>>> }
>>>> --------- end of file.h -------------
>>>>
>>>>
>>>> and a C file like this
>>>>
>>>> ----------- file.c ----------------
>>>>
>>>> ....
>>>> #include "file.h"
>>>>
>>>>
>>>> static void myFunc(S* pS)
>>>> {
>>>> ....
>>>> if (... == _S::a)
>>>> ....
>>>> ....
>>>> }
>>>>
>>>> -----------end of file.c --------------
>>>>
>>>> I'm using the Microsoft 2005 environment and so its compiler. The
>>>> compiler
>>>> says me that there is an error in myFunc, the error is that _S is an
>>>> undeclared indentifier. I'm not an expert of C but I don't know where
>>>> the
>>>> problem is.
>>>>
>>>> Can anyone help me, please?
>>>
>>> Roberto:
>>>
>>> Shouldn't that be
>>>
>>> S::a
>>>
>>> or
>>>
>>> struct _S::a
>>>
>>> ? Your code would have been OK in C++ (which does not need the typedef
>>> trick).
>>>
>>> --
>>> David Wilkinson
>>> Visual C++ MVP
>>
>> sorry, I don't understand what you told me. I know that in C++ all work
>> well, I tried it.
>>
>> Either S::a or _S::a are wrong?
>> in this case: if I want to keep the visibility of a, b variable as
>> enumerator inside my _S struct how I could do?
>> does C not have the operator :: to use in that way?
>
> roberto:
>
> I'm not an expert in C either. My original point was that the C compiler
> does not understand the symbol S_ alone (without the word struct), so you
> should use the typedef'd name S, or the full name struct S_.
>
> But a little research reveals that C does not have the scope resolution
> operator ::. Have you tried without the scope resolution?
>
> Are you sure it is legal C to declare an enum inside a struct definition?
> Why not just declare the struct outside the struct? (Different names for
> the enum items might be a good idea; I always use caps for enum items.)
>
> --
> David Wilkinson
> Visual C++ MVP

Hi David,

I think you can use an enum inside a struct as well as an union (that comes
from Kernighan book).
The workaround to solve the problem is to put the enum outside the struct,
and declare it as a typedef, after that you can declare
the variable tyep inside the struct _S as:

enum myenum
{
a,
b
};
typedef enum myenum myenum;


struct _S
{
...
myenum type;
...
};

so in the function and precisely in the if statetment you can use:

if (... == a ) /*or b*/

in that way all work fine. If the operator isn't available from C then all
is right, otherwise I don't unedrstand while the compiler told me that...

anyway thanks a lot for your help(also for Joe)

roberto



Re: struct & enum & :: visibility operator by robbio

robbio
Thu Jul 17 16:07:11 CDT 2008


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:ul%23y48E6IHA.3684@TK2MSFTNGP05.phx.gbl...
> robbio <roberto674@supereva.it> wrote:
>> I have a header file like this:
>>
>> -------- file.h ------------
>> ...
>> extern "C"
>> {
>> ...
>> typedef struct _S S;
>> ...
>>
>> struct _S
>> {
>> ...
>> enum
>> {
>> a,
>> b
>> }myenum;
>> ...
>> };
>> }
>> --------- end of file.h -------------
>>
>>
>> and a C file like this
>>
>> ----------- file.c ----------------
>>
>> ....
>> #include "file.h"
>>
>>
>> static void myFunc(S* pS)
>> {
>> ....
>> if (... == _S::a)
>> ....
>> ....
>> }
>
> There ain't no such thing as a scope resolution operator (::) in C. Just
> write "a" instead of _S::a
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925

Hi Igor,

the good news is the it worked. So my question is How it could be possible
to see and use an enumarator member outside that struct (where it is
declared)?
In that way it's not useful to declare the enum inside the struct, or at
least it has nomeaning, because the enumarator "lives" also outside the
struct and so it's visible outside, is it right?


thanks a lot
roberto



Re: struct & enum & :: visibility operator by David

David
Thu Jul 17 16:17:34 CDT 2008

robbio wrote:
> the good news is the it worked. So my question is How it could be possible
> to see and use an enumarator member outside that struct (where it is
> declared)?
> In that way it's not useful to declare the enum inside the struct, or at
> least it has nomeaning, because the enumarator "lives" also outside the
> struct and so it's visible outside, is it right?

roberto:

It seems that C does not consider the enum to be declared inside the struct.
Although you are allowed to do it, it is exactly the same as if you had declared
it outside the struct.

Something learned today. But really, I don't have any use for C, so I will
likely forget it again.

--
David Wilkinson
Visual C++ MVP

Re: struct & enum & :: visibility operator by Igor

Igor
Thu Jul 17 16:17:39 CDT 2008

robbio <roberto674@supereva.it> wrote:
>>> struct _S
>>> {
>>> enum
>>> {
>>> a,
>>> b
>>> }myenum;
>>> };
>>> }
>>
>> There ain't no such thing as a scope resolution operator (::) in C.
>> Just write "a" instead of _S::a
>
> the good news is the it worked. So my question is How it could be
> possible to see and use an enumarator member outside that struct
> (where it is declared)?

There ain't no such thing as class scope in C, the way there is in C++.
You can declare names inside a struct, but they live in the nearest
enclosing file or block scope.

> In that way it's not useful to declare the enum inside the struct, or
> at least it has nomeaning, because the enumarator "lives" also
> outside the struct and so it's visible outside, is it right?

Quite.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: struct & enum & :: visibility operator by Alexander

Alexander
Thu Jul 17 23:29:17 CDT 2008


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:e1kRsOF6IHA.1592@TK2MSFTNGP04.phx.gbl...
>
> There ain't no such thing as class scope in C, the way there is in C++.
> You can declare names inside a struct, but they live in the nearest
> enclosing file or block scope.
>
>> In that way it's not useful to declare the enum inside the struct, or
>> at least it has nomeaning, because the enumarator "lives" also
>> outside the struct and so it's visible outside, is it right?
>
> Quite.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>

On the other hand, structures in C first declared in a function declaration
argument are not the same as declared in file scope... In C++, it's OK. If
I'm not mistaken, of course.



Re: struct & enum & :: visibility operator by Hendrik

Hendrik
Fri Jul 18 02:46:31 CDT 2008

Alexander Grigoriev wrote:
> [...]
> On the other hand, structures in C first declared in a function declaration
> argument are not the same as declared in file scope... In C++, it's OK. If
> I'm not mistaken, of course.

I might be dense here, but I just have to ask:
How do you declare a struct in a function decl?

Schobi

Re: struct & enum & :: visibility operator by Joe

Joe
Fri Jul 18 03:09:33 CDT 2008

On Jul 18, 2:32=A0am, "robbio" <roberto...@supereva.it> wrote:
> Hi at all,
>
> I have a header file like this:
>
> -------- file.h ------------
> ...
> extern "C"
> {
> =A0 =A0 ...
> =A0 =A0 typedef struct _S S;
> =A0 =A0 ...
>
> =A0 =A0 struct _S
> =A0 =A0 {
> =A0 =A0 =A0 =A0 ...
> =A0 =A0 =A0 =A0 enum
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 a,
> =A0 =A0 =A0 =A0 =A0 =A0 b
> =A0 =A0 =A0 =A0 }myenum;
> =A0 =A0 =A0 =A0 ...
> =A0 =A0 };}
>
> --------- end of file.h -------------
>
> and a C file like this
>
> ----------- file.c ----------------
>
> ....
> #include "file.h"
>
> static void myFunc(S* pS)
> {
> =A0 =A0 ....
> =A0 =A0 if (... =3D=3D _S::a)
> =A0 =A0 =A0 =A0 ....
> =A0 =A0 ....
>
> }
>
> -----------end of file.c --------------
>
> I'm using the Microsoft 2005 environment and so its compiler. The compile=
r
> says me that there is an error in myFunc, the error is that _S is an
> undeclared indentifier. I'm not an expert of C but I don't know where the
> problem is.
>
> Can anyone help me, please?
>
> thanks a lot
> roberto

I think the :: operator only in C++ not in C, so u should write like
the following:
if (a =3D=3D pS->myenum)

Re: struct & enum & :: visibility operator by Ulrich

Ulrich
Fri Jul 18 06:47:03 CDT 2008

Hendrik Schober wrote:
> How do you declare a struct in a function decl?

Like this:

void foo( struct bar* b);

Typically, that code is unwanted, and you rather want this:

struct bar;
...
void foo( struct bar* b);

I know that some compilers warn about this (though I don't remember why) and
as a feature it is even less useful than goto i.e. I haven't found any use
for it at all yet.

Uli


--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Re: struct & enum & :: visibility operator by Igor

Igor
Fri Jul 18 06:57:08 CDT 2008

"Hendrik Schober" <Spamtrap@gmx.de> wrote in message
news:egXulpK6IHA.1196@TK2MSFTNGP05.phx.gbl
> Alexander Grigoriev wrote:
>> [...]
>> On the other hand, structures in C first declared in a function
>> declaration argument are not the same as declared in file scope...
>> In C++, it's OK. If I'm not mistaken, of course.
>
> I might be dense here, but I just have to ask:
> How do you declare a struct in a function decl?

void f(struct S{int x;} *p);

Pretty much the only way to actually call such a function is f(NULL),
since the name S is not visible anywhere.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: struct & enum & :: visibility operator by Hendrik

Hendrik
Fri Jul 18 07:23:23 CDT 2008

Thanks to Uli and you!

Igor Tandetnik wrote:
> "Hendrik Schober" <Spamtrap@gmx.de> wrote in message
> news:egXulpK6IHA.1196@TK2MSFTNGP05.phx.gbl
>> Alexander Grigoriev wrote:
>>> [...]
>>> On the other hand, structures in C first declared in a function
>>> declaration argument are not the same as declared in file scope...
>>> In C++, it's OK. If I'm not mistaken, of course.
>> I might be dense here, but I just have to ask:
>> How do you declare a struct in a function decl?
>
> void f(struct S{int x;} *p);
>
> Pretty much the only way to actually call such a function is f(NULL),
> since the name S is not visible anywhere.

...except in the function, you mean?
Mhmm. Still, it wouldn't be very useful.

Thanks anyway. I hadn't known that.

Schobi

Re: struct & enum & :: visibility operator by Alexander

Alexander
Fri Jul 18 10:14:59 CDT 2008


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:O5Ws84M6IHA.5116@TK2MSFTNGP06.phx.gbl...
>>
>> I might be dense here, but I just have to ask:
>> How do you declare a struct in a function decl?
>
> void f(struct S{int x;} *p);
>
> Pretty much the only way to actually call such a function is f(NULL),
> since the name S is not visible anywhere.
> --

I'm not sure if you can do complete declaration there. I was talking about
incomplete declaration AKA "forward". Which, of course, becomes then not so
"forward" but more like "inward".



Re: struct & enum & :: visibility operator by Igor

Igor
Fri Jul 18 11:25:56 CDT 2008

"Alexander Grigoriev" <alegr@earthlink.net> wrote in message
news:elaSMkO6IHA.1468@TK2MSFTNGP05.phx.gbl
> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
> news:O5Ws84M6IHA.5116@TK2MSFTNGP06.phx.gbl...
>>>
>>> I might be dense here, but I just have to ask:
>>> How do you declare a struct in a function decl?
>>
>> void f(struct S{int x;} *p);
>>
>> Pretty much the only way to actually call such a function is f(NULL),
>> since the name S is not visible anywhere.
>
> I'm not sure if you can do complete declaration there.

Comeau compiles the above in C99 mode.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: struct & enum & :: visibility operator by robbio

robbio
Fri Jul 18 14:10:44 CDT 2008


"Joe.M" <cholyjoe@gmail.com> wrote in message
news:ddaab242-9f68-4b47-849e-135c4d538141@c2g2000pra.googlegroups.com...
On Jul 18, 2:32 am, "robbio" <roberto...@supereva.it> wrote:
> Hi at all,
>
> I have a header file like this:
>
> -------- file.h ------------
> ...
> extern "C"
> {
> ...
> typedef struct _S S;
> ...
>
> struct _S
> {
> ...
> enum
> {
> a,
> b
> }myenum;
> ...
> };}
>
> --------- end of file.h -------------
>
> and a C file like this
>
> ----------- file.c ----------------
>
> ....
> #include "file.h"
>
> static void myFunc(S* pS)
> {
> ....
> if (... == _S::a)
> ....
> ....
>
> }
>
> -----------end of file.c --------------
>
> I'm using the Microsoft 2005 environment and so its compiler. The compiler
> says me that there is an error in myFunc, the error is that _S is an
> undeclared indentifier. I'm not an expert of C but I don't know where the
> problem is.
>
> Can anyone help me, please?
>
> thanks a lot
> roberto

I think the :: operator only in C++ not in C, so u should write like
the following:
if (a == pS->myenum)
--------------------------------

I know that in C++ it works, because a struct is a particular instance of a
class with all members public by default and the scopre operator :: is
valid.
I wanted use _S::a or _S::b into the if stament.
I think the problem is conceptual, that is: a declaration like that, struct
_S {...}; is only a declaration of a type like int or char or similar,so you
cannot use a type in an expression like that in the if stament, so the
compiler tried to search for a reasonable variable _S to use into the if
statement and it couldn't find, because struct _S is not a valid expression
to put into it, maybe...

thanks a lot anyway
roberto



Re: struct & enum & :: visibility operator by Hendrik

Hendrik
Fri Jul 18 15:15:45 CDT 2008

Igor Tandetnik wrote:
> "Alexander Grigoriev" <alegr@earthlink.net> wrote in message
> news:elaSMkO6IHA.1468@TK2MSFTNGP05.phx.gbl
>> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
>> news:O5Ws84M6IHA.5116@TK2MSFTNGP06.phx.gbl...
>>>> I might be dense here, but I just have to ask:
>>>> How do you declare a struct in a function decl?
>>> void f(struct S{int x;} *p);
>>>
>>> Pretty much the only way to actually call such a function is f(NULL),
>>> since the name S is not visible anywhere.
>> I'm not sure if you can do complete declaration there.
>
> Comeau compiles the above in C99 mode.

Ah. So that's C, only?
<checks>
Right. It complains "error: type definition is not allowed"
otherwise.

Schobi

Re: struct & enum & :: visibility operator by Igor

Igor
Fri Jul 18 16:09:33 CDT 2008

Hendrik Schober <Spamtrap@gmx.de> wrote:
> Igor Tandetnik wrote:
>> "Alexander Grigoriev" <alegr@earthlink.net> wrote in message
>> news:elaSMkO6IHA.1468@TK2MSFTNGP05.phx.gbl
>>> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
>>> news:O5Ws84M6IHA.5116@TK2MSFTNGP06.phx.gbl...
>>>>> I might be dense here, but I just have to ask:
>>>>> How do you declare a struct in a function decl?
>>>> void f(struct S{int x;} *p);
>>>>
>>>> Pretty much the only way to actually call such a function is
>>>> f(NULL), since the name S is not visible anywhere.
>>> I'm not sure if you can do complete declaration there.
>>
>> Comeau compiles the above in C99 mode.
>
> Ah. So that's C, only?
> <checks>
> Right. It complains "error: type definition is not allowed"
> otherwise.

Correct. C++ disallows it in 8.3.5/6: ...Types shall not be defined in
return or parameter types.

I suspect it's allowed in C by accident, not by design.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: struct & enum & :: visibility operator by Alexander

Alexander
Fri Jul 18 21:47:59 CDT 2008


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:Omi4ssR6IHA.3856@TK2MSFTNGP06.phx.gbl...
>>
>> Ah. So that's C, only?
>> <checks>
>> Right. It complains "error: type definition is not allowed"
>> otherwise.
>
> Correct. C++ disallows it in 8.3.5/6: ...Types shall not be defined in
> return or parameter types.
>
> I suspect it's allowed in C by accident, not by design.
> --

I think it was introduced intentionally, along with other GCC niceties.



Re: struct & enum & :: visibility operator by Joe

Joe
Mon Jul 21 08:12:06 CDT 2008

"robbio" <roberto674@supereva.it> wrote in news:OiTe13E6IHA.2348
@TK2MSFTNGP06.phx.gbl:

Ok, here is the corrected form. (It's been a which since I did C!)

t.h

====
#ifndef T_H
#define T_H
enum myenum
{
a,
b
} ;
typedef struct _S
{

enum myenum i;
} S;

#endif // T_H


===
t.c

===

#include "t.h"


static void myFunc(S*pS)
{
if (pS->i == a)
{
puts("a");
}
}

int main()
{
S s;
s.i = a;
myFunc(&s);
}

====


In C, you can't embed types so the enum has to come outside the struct.


HTH

joe

PS I actually compiled and ran it, so I know it actually works. There
is not scoping operator in C, thus no :: stuff.


Re: struct & enum & :: visibility operator by Joe

Joe
Sun Aug 03 22:17:57 CDT 2008

On Jul 19, 3:10=A0am, "robbio" <roberto...@supereva.it> wrote:
> "Joe.M" <choly...@gmail.com> wrote in message
>
> news:ddaab242-9f68-4b47-849e-135c4d538141@c2g2000pra.googlegroups.com...
> On Jul 18, 2:32 am, "robbio" <roberto...@supereva.it> wrote:
>
>
>
> > Hi at all,
>
> > I have a header file like this:
>
> > -------- file.h ------------
> > ...
> > extern "C"
> > {
> > ...
> > typedef struct _S S;
> > ...
>
> > struct _S
> > {
> > ...
> > enum
> > {
> > a,
> > b
> > }myenum;
> > ...
> > };}
>
> > --------- end of file.h -------------
>
> > and a C file like this
>
> > ----------- file.c ----------------
>
> > ....
> > #include "file.h"
>
> > static void myFunc(S* pS)
> > {
> > ....
> > if (... =3D=3D _S::a)
> > ....
> > ....
>
> > }
>
> > -----------end of file.c --------------
>
> > I'm using the Microsoft 2005 environment and so its compiler. The compi=
ler
> > says me that there is an error in myFunc, the error is that _S is an
> > undeclared indentifier. I'm not an expert of C but I don't know where t=
he
> > problem is.
>
> > Can anyone help me, please?
>
> > thanks a lot
> > roberto
>
> I think the :: operator only in C++ not in C, so u should write like
> the following:
> if (a =3D=3D pS->myenum)
> --------------------------------
>
> I know that in C++ it works, because a struct is a particular instance of=
a
> class with all members public by default and the scopre operator :: is
> valid.
> =A0I wanted use _S::a or _S::b into the if stament.
> I think the problem is conceptual, that is: a declaration like that, stru=
ct
> _S {...}; is only a declaration of a type like int or char or similar,so =
you
> cannot use a type in an expression like that in the if stament, so the
> compiler tried to search for a reasonable variable _S to use into the if
> statement and it couldn't find, because struct _S is not a valid expressi=
on
> to put into it, maybe...
>
> thanks a lot anyway
> roberto

Hi,

I know what's u want to do, u are missing my code meaning. So I paste
the demo here:

typedef struct _S S;

struct _S {
enum {
a,
b
} myenum;
};

int _tmain(int argc, _TCHAR* argv[])
{
S s1;
s1.myenum =3D a;

if (s1.myenum =3D=3D a)
_tprintf_s(_T("is ok!\n"));
else
_tprintf_s(_T("is failed!\n"));

return 0;
}

summary:
1. In c, the enum is the global const int.
2. _S is just the type's tag no other meanings.