Posted by Andrew Mann on 2000-02-08
-----Original Message-----
From: Andrew Mann [mailto:amann@xxxxx.xxx]
Sent: Tuesday, February 08, 2000 3:36 AM
To: tcg@xxxx.xx.xx
Subject: RE: TCG digest 680
Basic C++ courses are overrated. I have never met a teacher or professor
that knows enough about the language, the platform in which they claim
expertise, or even the general concepts of programming to hack it in a non
teaching position. If you can't do, teach - eh? You shouldn't flame so
much - because what you say is not entirely correct either. The statements
you have below are dependant on the compiler and not on the language. If
you were to go into language specifics you'd have to specify something like
ANSI C/C++. As it is, your statements seem to be based on what gcc churns
out. Maybe Microsoft's latest compiler does the same - I'm not entirely
sure.
There's really only one way to use a struct command.
struct <optional structure identifier> { members }
You can list the identifier or not. In general you will want to use the
identifier for linked lists. This is not an absolute requirement. The
typedef command is a completely seperate command of the form
typedef (valid data structure) (new name)
Thus typedef int mytype is a valid command. typedef struct { yadda
yadda } bob simply says that the structure can now be referenced as "bob"
instead of struct { yadda yadda} (or struct optionalname if you gave it an
optional name).
The "third form" is the same structure form, with a variable name after it.
In this case the struct (as in the typedef example) is used as a data type.
S3 is the variable name.
The linked list examples are erroneous. This depends on wether you are
using a single-pass compiler or a multi-pass compiler. GCC is (last i
checked) a single pass compiler. This means it scans the file in a top-down
fashion once. The reason
typedef struct {
S7* ptr;
} S7;
does not work for you is because when the compiler reaches the innards of
this struct it has not finished the struct (obviously) and thus has not
finished the typedef, and thus does not know what a S7 is. So when you try
to use a variable of the type pointer to S7 it has no idea how to handle
this. A multi-pass compiler would (should) leave this as a unresolved
symbol, continue to parse the file, and on a later pass it will be able to
complete the structure.
I'm not sure if your first example of linked lists will work in any
situation. If so, it's really the nice designers of the compiler and not
your coding. The correct form would be:
struct S6 {
struct S6* ptr;
};
Note that without a typedef the name S6 is not really usable as a valid
variable type. Since a typedef requires a completed structure in this case
you really are stuck using this form. Alternatively you could use a void
pointer and typecast it in the code, or for that matter just use a class
instead of a struct.
Glancing back over your code I would be VERY surprised if "S2 mine2;"
worked as well - for the same reason mentioned above.
Cheers,
AM
Contents:
RE: typedef struct vs struct - you are wrong (Emre Yucel
<exy1174@xxxxxxxxx.xxxx.xxx>)
Date: Mon, 07 Feb 2000 08:03:29 -0500
From: Emre Yucel <exy1174@xxxxxxxxx.xxxx.xxx>
NO NO NO, you guys are so bad at this stuff, most of you would probably
fail a basic C++ course. There are _3_ ways of using struct. (not two as
mentioned in the previous e-mails). Here are all 3 different ways in a
program, with comments. Hope this clears up confusion.
typedef struct {
int data;
int text;
} S1;
// This will define a typedef for S1, in both C and in C++
struct S2 {
int data;
int text;
};
// This will define a typedef for S2 ONLY in C++, will create error in C.
struct {
int data;
int text;
} S3;
// This will declare S3 to be a variable of type struct.
// This is VERY different from the above two.
// This does not define a type. The above two defined type S1 and S2.
// This tells compiler to allocate memory for variable S3
void main()
{
S1 mine1; // this will work, S1 is a typedef.
S2 mine2; // this will work, S2 is also a typedef.
S3 mine3; // this will NOT work, S3 is not a typedef.
S1.data = 5; // Will give error because S1 is only a typedef.
S2.data = 5; // Will also give error because S1 is only a typedef.
S3.data = 5; // This will work, because S3 is a variable.
}
// That's how they different stuff are handy.
also !!!!!!!!!!! This next bit is important for people who using linked
lists etc.
struct S6 {
S6* ptr;
};
// This works, in C++ only.
typedef struct {
S7* ptr;
} S7;
// Although you would think this does the same thing in C OR C++....
// This DOES NOT work in C nor C++ !!!
If you don't beleive me, copy and paste these program bits in your favorite
C or C++ compiler, you can see I am correct on this subject.
bye.
--
Emre Yucel
http://www.tiac.net/users/amoeba/ mailto:amoeba@xxxx.xxx
To signoff send a mail to listserver@xxxx.xx.xx with
"signoff tcg" in the body of your message.
Previous post | Next post | Timeline | Home