struct vs. typedef struct 
Author Message
 struct vs. typedef struct

could someone please tell me what putting a 'typedef' before 'struct'
does when i define a struct?

------------------------------------------------------------------------

"What a waste it is to lose one's mind. Or not to have a mind is being
very wasteful. How true that is." -- Dan Quayle during a fundraising
event for the United Negro College Fund.
------------------------------------------------------------------------



Sat, 04 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct


Quote:

>could someone please tell me what putting a 'typedef' before 'struct'
>does when i define a struct?

It causes a syntax error unless you also add an identifier to be declared.

Typedef is (at least syntactically) a storage class specifier. It causes
an identifier to be defined as a type name rather than declaring it as an
object.

For example:

        typedef int foo;        /* foo is now a synonym for the int type */

        typedef struct natalia nat;     /* nat is now synonym for struct
                                           natalia */

        nat n;  /* declare object n of type nat, which is really struct
                   natalia */

        foo i;  /* i is declared to be type int */

Typedef is used for convenience and for a little abstraction.
--



Sat, 04 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct

The typedef declaration causes the compiler to reconize a different name
for the variable type. I use to enhance readability in my code. There
may be other reasons also.

Jim

Quote:

> could someone please tell me what putting a 'typedef' before 'struct'
> does when i define a struct?

> ----------------------------
> -------------------------------------------

> ********************

> "What a waste it is to lose one's mind. Or not to have a mind is being

> very wasteful. How true that is." -- Dan Quayle during a fundraising
> event for the United Negro College Fund.
> ----------------------------------------
> -------------------------------



Sat, 04 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct

Quote:



> >could someone please tell me what putting a 'typedef' before 'struct'
> >does when i define a struct?

> It causes a syntax error unless you also add an identifier to be declared.

> Typedef is (at least syntactically) a storage class specifier. It causes
> an identifier to be defined as a type name rather than declaring it as an
> object.

> For example:

>         typedef int foo;        /* foo is now a synonym for the int type */

[SNIP]

There is also a bit of information on this in the c.l.c FAQ:

  2.1:    What's the difference between these two declarations?

                struct x1 { ... };
                typedef struct { ... } x2;

  A:      The first form declares a "structure tag"; the second declares
a
          "typedef".  The main difference is that you subsequently refer
          to the first type as "struct x1" and the second as "x2".  That
          is, the second declaration is of a slightly more abstract type
--
          its users don't necessarily know that it is a structure, and
          the keyword struct is not used when declaring instances of it.

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 05 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct

Groovy hepcat Natalia Rakowski was jivin' on 16 Sep 1997 19:20:33 GMT
in comp.lang.c.
struct vs. typedef struct's a cool scene! Dig it!

Quote:
>could someone please tell me what putting a 'typedef' before 'struct'
>does when i define a struct?

  It, in effect, creates a new type synonymous with your struct.
Actually it doesn't really create a new type, but it can be thought of
that way. For example:

struct foo
{
  int i;
  long l;
  char *cp;

Quote:
}

typedef struct foo bar;

  In this example, bar becomes another name for struct foo, and so it
can be thought of as a new type.

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Thu, 09 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct

Quote:

> >could someone please tell me what putting a 'typedef' before 'struct'
> >does when i define a struct?

>   It, in effect, creates a new type synonymous with your struct.
> Actually it doesn't really create a new type, but it can be thought of
> that way. For example:

> struct foo
> {
>   int i;
>   long l;
>   char *cp;
> }

> typedef struct foo bar;

Or better still:

typedef struct
        {
        int i;
        long l;
        char *cp;
        } foo;

Which means it behaves exactly like "struct foo", but you just drop the
"struct" in all your declarations.

--
Adrian Thewlis        O
The ghost who walks  /|)
                      <\



Fri, 10 Mar 2000 03:00:00 GMT  
 struct vs. typedef struct



           "Peter "Shaggy" Haywood" writes:

Quote:
>Groovy hepcat Natalia Rakowski was jivin' on 16 Sep 1997 19:20:33 GMT
>in comp.lang.c.
>struct vs. typedef struct's a cool scene! Dig it!

>>could someone please tell me what putting a 'typedef' before 'struct'
>>does when i define a struct?

>  It, in effect, creates a new type synonymous with your struct.
>Actually it doesn't really create a new type, but it can be thought of
>that way. For example:

It is wrong to think of typedef creating a new type because it quite
simply doesn't.

Quote:
>struct foo
>{
>  int i;
>  long l;
>  char *cp;
>}

This does create a new type called struct foo

Quote:
>typedef struct foo bar;
>  In this example, bar becomes another name for struct foo,

Right.

Quote:
>and so it can be thought of as a new type.

No that means that it *cannot* be though of as a new type, just another
name for an existing type. Thinking of it as a new (and hence separate)
type leads to the wrong conclusions e.g. consider:

    struct foo *p1 = 0;
    bar *p2 = p1;

If struct foo and bar were different types this would be illegal whereas
it is perfectly legal. Consider a case where there are different types:

    struct { int i; } *p3 = 0;
    struct { int i; } *p4 = p3;

Althouh the types of p3 and p4 look the same they are separate types as
far as C is concerned and the assignment/initialisation in the 2nd line
is illegal. Also consider a variant on your definitions:

typedef struct foo bar;

struct foo
{
  int i;
  long l;
  char *cp;

Quote:
}

Initially the type struct foo and hence also bar is incomplete. When
struct foo is completed later bar also becomes complete since it is just
a synonym for struct foo. Note this also demonstrates where they differ
in that the second line (struct foo) that introduces the complete type
declaration can't be replaced by bar, struct foo must be stated
explicitly. This demonstrates why structure tags are sometimes required
over typedef names.

--
-----------------------------------------


-----------------------------------------



Sun, 12 Mar 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Typedef structs within typedef structs...

2. Typedef structs within typedef structs...

3. struct vs typedef

4. struct vs. typedef

5. struct vs. typedef

6. typedef struct tagNAME {} NAME; vs. typedef struct NAME {};

7. missing struct vs zero length struct?

8. c# struct vs c++ struct

9. What the difference between "typedef struct" and "typedef struct tag_Struct"?

10. C++ memory access speeds: normal vs struct vs class

11. typedef struct not working under .NET C/C++ compiler

12. typedef declaration of struct

 

 
Powered by phpBB® Forum Software