Mail Archives: djgpp/1997/10/14/17:46:00
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 -