delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/14/17:46:00

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Difference between struct setups
Date: 14 Oct 1997 19:29:10 GMT
Organization: Oxford University, England
Lines: 74
Message-ID: <620h66$rj5$1@news.ox.ac.uk>
References: <199710130605 DOT TAA20415 AT fep1-orange DOT clear DOT net DOT nz>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Mon, 13 Oct 1997 18:02:46 GMT in comp.os.msdos.djgpp Jamie Love
(jamie DOT love AT clear DOT net DOT nz) wrote: 

: typedef struct X
:  {
:     .....
:  } X;
: ..
: X x;

: Now, when i was programming in borlands compiler, i just went:

: struct X
:  {
:    .....
:  };
: ..
: X x;

: So, what is the difference??

The second one won't work unless you're using a C++ compiler. A struct is
defined:

struct <identifier> { ..... };

It is referred to in your code (C) as `struct <identifier>', not just
`<identifier>'.

A typedef is a way of taking one type and giving it a new name, for
example:

typedef int thirtytwo_bit_integer;

After that, the type `thirtytwo_bit_integer' is identical to `int'. So the
first quoted struct definition is effectively:

struct <identifier> { ... };
typedef struct <identifier> <identifier>;

The effect is that you can now refer to the struct as just `<identifier>'
instead of `struct <identifier>'. You can do this anyway in C++, which is
presumably why your Borland compiler didn't give an error.

: I have trouble also understanding why i couldn't use the second setup in my
: c code in a small test program i made (it gave me many errors) while in
: another program i could go:

: struct X
:  {
:    .....
:  }y;

: why??

When declaring/defining a variable of struct type, you must prefix the
struct's identifier with `struct':

struct X y;

not

X y;

*Unless* you do the typedef above:

typedef struct X y;
X y;

is perfectly valid, provided struct X has been defined.

-- 
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019