[comp.lang.c] A question about typedef!

d0thomas@dtek.chalmers.se (Thomas Lundqvist) (11/20/90)

Hello! Could someone please tell me the difference between:

struct TEST { int a; };

typedef struct { int a; } TEST

--
Thomas Lundqvist, d0thomas@dtek.chalmers.se
Chalmers Institute of Technology, Gothenburg, SWEDEN

gwyn@smoke.brl.mil (Doug Gwyn) (11/21/90)

In article <1990Nov20.153907.12373@mathrt0.math.chalmers.se> d0thomas@dtek.chalmers.se (Thomas Lundqvist) writes:
>Hello! Could someone please tell me the difference between:
>struct TEST { int a; };
>typedef struct { int a; } TEST

The first declares a structure tag, the second (assuming you provide a
terminating semicolon) declares a synonym for a tagless structure.

K&R probably explains this.

schmidt@mars.jpl.nasa.gov (Kevin Schmidt) (11/21/90)

In article <1990Nov20.153907.12373@mathrt0.math.chalmers.se> d0thomas@dtek.chalmers.se (Thomas Lundqvist) writes:
>Hello! Could someone please tell me the difference between:
>
>struct TEST { int a; };
>
>typedef struct { int a; } TEST

Both structures are identical but references to the structure as a type are
different.  For the first case to declare a variable of that type you would
use:
	struct TEST x;
In the second case you would simply use:
	TEST x;

_______________________________________________________________________________
        _   ____    _   |                           |
       / / / _  \  / /  | Kevin T. Schmidt          | schmidt@mars.jpl.nasa.gov
      / / / /_/ / / /   | Jet Propulsion Laboratory | schmidt@jems.jpl.nasa.gov
 _   / / / ____/ / /    | 4800 Oak Grove Dr.        |
/ /_/ / / /     / /___  | M/S 301-355               |
\____/ /_/     /______/ | Pasadena, CA  91109       |

gordon@osiris.cso.uiuc.edu (John Gordon) (11/21/90)

d0thomas@dtek.chalmers.se (Thomas Lundqvist) writes:

>Hello! Could someone please tell me the difference between:

>struct TEST { int a; };

>typedef struct { int a; } TEST

	I think the difference is that if you use the first way, you will
always have to say "struct TEST" whenever you declare a var of that type and
the second way you can just say "TEST" without the "struct".

d0thomas@dtek.chalmers.se (Thomas Lundqvist) (11/21/90)

Well! Thank you all people who have replied my question (including those who
have sent E-mail). I think I know the difference now.

I will perhaps use the typedef form, because it's shorter. But the other
form with a struct-tag is perhaps more informative because you have to
write struct everywhere and then you wont have to investigate if its a
struct or not...

Bye, and thanks!

Thomas Lundqvist, d0thomas@dtek.chalmers.se
Chalmers Institute of Technology, Gothenburg, SWEDEN

prk@planet.bt.co.uk (KnightRider) (11/22/90)

d0thomas@dtek.chalmers.se (Thomas Lundqvist) writes:

>Hello! Could someone please tell me the difference between:

>struct TEST { int a; };

To use this later on, you have to use this code:

struct TEST var1, *var2;

>typedef struct { int a; } TEST
To use this, you can do:

TEST var1,*var2;

>--
>Thomas Lundqvist, d0thomas@dtek.chalmers.se
>Chalmers Institute of Technology, Gothenburg, SWEDEN

Peter Knight 
BT Research

#include <std.disclaimer>

mbrennan@swift.cs.tcd.ie (11/22/90)

In article <1990Nov20.153907.12373@mathrt0.math.chalmers.se>, d0thomas@dtek.chalmers.se (Thomas Lundqvist) writes:
> Hello! Could someone please tell me the difference between:
> 
> (1)  struct TEST { int a; };
> 
> (2)  typedef struct { int a; } TEST

With case (1) you would delcare a variable:    struct TEST foo ;
but with case (2) you would write              TEST foo ;

In case (1) the words "struct TEST" together are a type, in the same way
as int, char long etc are types, but with case (2) the word "TEST" on it's
own is a type.
-- 
 ,   ,  ,      ,              ,       ,          ,          , ,      ,
Micheal O Braonain     Roinn Riomheolaiochta, Colaiste Na Trinoide, BAC 2.
Email                  mbrennan@cs.tcd.ie