![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
my compiler says I have to have a variable at the end of my structureI'm just started to write C++ programs, and my compiler says I have to have a variable at the end of my structure:
CPP / C++ / C Code:
The book that I was using (is a piece of trash, and) doesnt mention a variable there when it teaches about structures. What is this?!?!?!?!? Last edited by JdS : 21-Feb-2004 at 08:27.
Reason: Please use a better title in your threads
|
|
#2
|
||||
|
||||
|
Quote:
This creates a variable type called person that is made up of your structure. This is not your variable at this point just a type, like int. Then it goes on to say that you want to creat a variable of this type. This is a short-hand way to write: CPP / C++ / C Code:
Does that help or is it that more confusing? EDIT: Ambeco, I noticed that you changed the name of this thread. For the record, I have never seen a compiler require a variable after the definition of a structure, but it does require a semicolon. I have had many a compile error because of this little detail ![]() Last edited by dsmith : 21-Feb-2004 at 10:12.
Reason: Added information due to title change
|
|
#3
|
||||
|
||||
|
Quote:
As dsmith said, your struct person is in effect creating a new type, like int. But if you want to create a "type" you use: CPP / C++ / C Code:
CPP / C++ / C Code:
But in your case you were simply defining a variable with the specified structure, not making a type. If you leave off typedef you need to specify the variable itself: CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
I think I figured it outQuote:
|
|
#5
|
||||
|
||||
|
Hey Walt, I have a question for you. Let's say I define a structure like this:
CPP / C++ / C Code:
Then I create a variable with it: CPP / C++ / C Code:
What is the difference between doing what I did, and adding the typedef statement before my structure definition? |
|
#6
|
||||
|
||||
|
Well, other than the creation of the variable showing the programmer's healthy ego
According to my tests: CPP / C++ / C Code:
By creating a typedef: CPP / C++ / C Code:
CPP / C++ / C Code:
I haven't checked whether there is a difference in the actual execuatable, but I doubt there is. All the test files I created (4) to see what happens are identical in size. If I have time, I'll run these thru VC6, too, but that's on a different machine. FYI, Tubo 1.0 created files 8K is size, Borland 5.5 files were 55K. PS -- I just looked at my first post and I had the syntax wrong on the [i]typedef[i]inition. Follow this post. It was tested by the compiler. |
|
#7
|
||||
|
||||
|
My experience has been that C++ allows a variable definition from a struct whereas straight C does not.
So in C++, this works: CPP / C++ / C Code:
Whereas in a straight C compiler it doesn't. YMMV. It may be that the newer C++ compilers put in an implicit "struct" when it sees this. |
|
#8
|
||||
|
||||
|
Yes, that's what I was thinking. I'm running Borland C++ builder 6 right now, and I have never had a problem doing what I did in my example above, which is why I asked. Should I make it a habit to perform a typedef statement, or just take advantage of the C++ compiler feature? I think I'll try to do it in VC6 and see what happens.
|
|
#9
|
||||
|
||||
|
Interesting. I didn't realize C++ allows you to leave the struct off the definition. I was compiling as C.
|
|
#10
|
||||
|
||||
|
Quote:
For portability reasons, it would not be a bad idea to have a typedef statement. If this really is a C++ "feature" and you are writing C++ code it probably doesn't really matter. For what it's worth, I have gotten lazy and ussually don't use the typedef or the explicit struct, but this is for C++ programs. Of the three compilers I use, none of them complain (Visual C++, gnu g++, bloodshed dev-c++). |
Recent GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| question of practice | magiccreative | C++ Forum | 1 | 06-Feb-2004 07:17 |
| newbie with stupid questions | JUNK KED | MySQL / PHP Forum | 1 | 16-Oct-2003 08:58 |
| a C input question.. | tmike | C Programming Language | 2 | 19-Sep-2003 02:39 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
| a noobish compiler question | Charunks | C++ Forum | 5 | 03-Sep-2003 02:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The