Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   typedef struct (http://www.velocityreviews.com/forums/t754940-typedef-struct.html)

John McDermick 10-15-2011 06:38 PM

typedef struct
 
Hello,

If I have this definition in a header file:

typedef struct A
{
int test;
} B;

What exactly is A and what is B ?

What does it mean when I declare like this:

typedef struct A
{
int test;
};

And what does it mean when I declare like this:

typedef struct
{
int test;
} B;


Eric Sosman 10-15-2011 07:40 PM

Re: typedef struct
 
On 10/15/2011 2:38 PM, John McDermick wrote:
> Hello,
>
> If I have this definition in a header file:
>
> typedef struct A
> {
> int test;
> } B;
>
> What exactly is A and what is B ?


`A' is a "struct tag," something that distinguishes one particular
kind of struct from other kinds. The combination `struct A' names the
type of that particular kind of struct, so you can use that name in
other contexts like `struct A instance;' to declare a variable of that
struct type or `struct A *pointer;' to declare a variable that points
to objects of that type. `A' by itself means nothing in C; the rules
in C++ are different.

`B' is an alias for the type `struct A', and can be used wherever
`struct A' could. So `B instance;' declares a variable of the struct
type, and `B *pointer;' declares a variable that can point to it. `B'
and `struct A' are interchangeable.

> What does it mean when I declare like this:
>
> typedef struct A
> {
> int test;
> };


It means you declare `struct A' and `A' with the meanings as above.
Then you start to declare an alias for `struct A' but never provide the
alias' name. I'm not sure the Standard requires a diagnostic for this
vacuous attempt at an alias, but compilers are likely to warn about it
in any case. It's a lot like writing `typedef float;' and never giving
the name that you want to use as an alias for `float'.

> And what does it mean when I declare like this:
>
> typedef struct
> {
> int test;
> } B;


Again you declare a struct type, but this one has no name of its
own: There is no way to complete `struct ???' to name the type of
this struct. However, you also declare `B' as an alias for that
anonymous struct type, so you can still write `B instance;' and
`B *pointer;'. So, `B' is an alias for a type that has no true name
of its own, but `B' will serve as a perfectly good substitute.

--
Eric Sosman
esosman@ieee-dot-org.invalid

BartC 10-15-2011 07:42 PM

Re: typedef struct
 


"John McDermick" <johnthedspguy@gmail.com> wrote in message
news:ff89df4d-151e-4537-bb10-af6d148475b4@n13g2000vbv.googlegroups.com...
> Hello,
>
> If I have this definition in a header file:
>
> typedef struct A
> {
> int test;
> } B;
>
> What exactly is A and what is B ?


It gets more confusing when you write it like this:

typedef struct A
{
int test;
} A;

Both A and A can co-exist, because the first A (A in your example) needs to
be used as:

struct A x;

while the second A (B in your example) can be written like this:

A x;

> What does it mean when I declare like this:
>
> typedef struct A
> {
> int test;
> };


This one puzzles me too; adding the 'typedef' doesn't buy you much more than
just writing struct A{int test;}. In fact, gcc will complain about it so it
agrees with me!

> And what does it mean when I declare like this:
>
> typedef struct
> {
> int test;
> } B;


This declares a type alias for struct{int test;}, called B.

--
Bartc


Keith Thompson 10-15-2011 11:26 PM

Re: typedef struct
 
John McDermick <johnthedspguy@gmail.com> writes:
> If I have this definition in a header file:
>
> typedef struct A
> {
> int test;
> } B;
>
> What exactly is A and what is B ?
>
> What does it mean when I declare like this:
>
> typedef struct A
> {
> int test;
> };
>
> And what does it mean when I declare like this:
>
> typedef struct
> {
> int test;
> } B;


Eric has ably answered your question.

Opinions differ on this, but personally I don't feel the need for a
typedef at all. I'd just declare the type as

struct A {
int test;
};

and then refer to it as "struct A". There's no real need to use a
typedef to give the type another name; it already has a perfectly good
one.

(The counterargument is that many people like to have a one-word name
for the type.)

You can also use the same identifier for both the tag and the typedef:

typedef struct A {
int test;
} A;

You can then refer to the type either as "struct A" or as "A". Since
struct tags are in a different namespace than typedef names (since the
tag can only appear immediately after the keyword "struct', and the
typedef name never can), there's no conflict.

(Note that C++ makes the struct tag usable directly as a type name; even
without the typedef, you can refer to the type as just "A". C doesn't
do this.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

John McDermick 10-16-2011 04:48 PM

Re: typedef struct
 
Awesome and easy to understand explanations!

Thank you to all of you :o)

Kulin 05-31-2012 07:10 PM

Re: typedef struct
 
Keith Thompson <kst-u@mib.org> wrote:

> John McDermick <johnthedspguy@gmail.com> writes:
> > If I have this definition in a header file:
> >
> > typedef struct A
> > {
> > int test;
> > } B;
> >
> > What exactly is A and what is B ?


What if I want to create a pointer to the struct inside the struct?

typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors. Is it possible to create a pointer to the
struct being defined, within the struct, using the original typedef and
thereby not having to code 'struct' to define the pointer?

> struct A {
> struct A *p;
> };



> and then refer to it as "struct A". There's no real need to use a
> typedef to give the type another name; it already has a perfectly good
> one.
>
> (The counterargument is that many people like to have a one-word name
> for the type.)


My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.




Eric Sosman 05-31-2012 07:14 PM

Re: typedef struct
 
On 5/31/2012 3:10 PM, Kulin wrote:
> Keith Thompson<kst-u@mib.org> wrote:
>
>> John McDermick<johnthedspguy@gmail.com> writes:
>>> If I have this definition in a header file:
>>>
>>> typedef struct A
>>> {
>>> int test;
>>> } B;
>>>
>>> What exactly is A and what is B ?

>
> What if I want to create a pointer to the struct inside the struct?
>
> typedef struct A {
> A *p1; /* not allowed */
> B *p2; /* not allowed */
> } B;
>
> This gives compilation errors. Is it possible to create a pointer to the
> struct being defined, within the struct, using the original typedef and
> thereby not having to code 'struct' to define the pointer?
>
>> struct A {
>> struct A *p;
>> };

>
>
>> and then refer to it as "struct A". There's no real need to use a
>> typedef to give the type another name; it already has a perfectly good
>> one.
>>
>> (The counterargument is that many people like to have a one-word name
>> for the type.)

>
> My desire is simply to avoid having to create pointers as struct * inside
> the structure. Other than that I would tend to agree.


typedef struct A B; // "incomplete type" declaration
struct A { // "completes" the type `struct A'
int test;
B *p1;
};

--
Eric Sosman
esosman@ieee-dot-org.invalid

Xavier Roche 05-31-2012 07:14 PM

Re: typedef struct
 
Le 31/05/2012 21:10, Kulin a écrit :
> What if I want to create a pointer to the struct inside the struct?
> typedef struct A {
> A *p1; /* not allowed */
> B *p2; /* not allowed */
> } B;


Hum, why don't you first define the type incompletely ?

typedef struct A A;
struct A {
A *p1;
A *p2;
};

Keith Thompson 05-31-2012 09:24 PM

Re: typedef struct
 
Kulin <remailer@reece.net.au> writes:
[...]
> What if I want to create a pointer to the struct inside the struct?
>
> typedef struct A {
> A *p1; /* not allowed */
> B *p2; /* not allowed */
> } B;
>
> This gives compilation errors. Is it possible to create a pointer to the
> struct being defined, within the struct, using the original typedef and
> thereby not having to code 'struct' to define the pointer?

[snip]
> My desire is simply to avoid having to create pointers as struct * inside
> the structure. Other than that I would tend to agree.


Why do you want to avoid using the "struct" keyword?

But if you want to use a typedef, there's no real need to use different
identifiers for the struct tag and the typedef name. This:

typedef struct A A;

is perfectly valid and doesn't create any potential ambiguity. Or at
least use a consistent convention, like:

typedef struct A_ A;

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Nomen Nescio 06-01-2012 12:50 AM

Re: typedef struct
 
Thank you very much!

Carl



All times are GMT. The time now is 01:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.