GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2004, 10:41
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Question

my compiler says I have to have a variable at the end of my structure


I'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:
struct person
{
      int height;
}variable;  <------- what is this variable

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  
Old 20-Feb-2004, 11:37
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
CPP / C++ / C Code:
struct person
{
      int height;
}variable;  <------- what is this variable

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?!?!?!?!?

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:
struct person{         //define the structure (type)
      int height;
};

struct person variable;  //Make a variable of this type (struct person)

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  
Old 20-Feb-2004, 13:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ambeco
I'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:
struct person
{
      int height;
}variable;  <------- what is this variable

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?!?!?!?!?

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:
typedef struct PERSON
{
      int height;
};
Now you can use PERSON the same way you use int
CPP / C++ / C Code:
PERSON person, people[5];  // define 2 vars with the PEOPLE type
I reccommend making the type name all caps as shown.

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:
struct PERSON
{
      int height;
} person, preople[5];  // define 2 vars with this structure
  #4  
Old 23-Feb-2004, 10:17
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Talking

I think I figured it out


Quote:
Originally Posted by WaltP
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:
struct PERSON
{
      int height;
} person, preople[5];  // define 2 vars with this structure
I dont think you guys understood my question, but the part I have marked in the quote answered my question. The two words right after the } are new structures of the person type. If I want to leave them off I have to use typedef. Got it. Thanks for your help guys!
  #5  
Old 23-Feb-2004, 14:03
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Hey Walt, I have a question for you. Let's say I define a structure like this:
CPP / C++ / C Code:
struct student
{
    char *name;
    float gpa;
    int score[3];
};

Then I create a variable with it:
CPP / C++ / C Code:
student aaron;

What is the difference between doing what I did, and adding the typedef statement before my structure definition?
  #6  
Old 23-Feb-2004, 21:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Well, other than the creation of the variable showing the programmer's healthy ego , it doesn't compile -- at least not in Borland 5.5 and Turbo 1.0

According to my tests:
CPP / C++ / C Code:
struct STRUCTURE1
{
    int  val1a;
    int  val1b;
};
struct STRUCTURE1 test1b;
STRUCTURE1 test1a;  // declaration syntax error
without the typedef you must specify the struct structname when defining the variable otherwise a declaration syntax error occurs.

By creating a typedef:
CPP / C++ / C Code:
typedef struct 
{
    int  val4a;
    int  val4b;
} STRUCTURE4;
STRUCTURE4 test4a;
or
CPP / C++ / C Code:
struct structure3 
{
    int  val3a;
    int  val3b;
};
typedef struct structure3 STRUCTURE3;
STRUCTURE3 test3a;
you don't need the struct qualifier when defining the variable.

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  
Old 23-Feb-2004, 22:04
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
struct STRUCTURE1
{
    int  val1a;
    int  val1b;
};
STRUCTURE1 test1a;

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  
Old 23-Feb-2004, 22:36
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 23-Feb-2004, 23:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Interesting. I didn't realize C++ allows you to leave the struct off the definition. I was compiling as C.
  #10  
Old 24-Feb-2004, 07:31
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by aaroncohn
Should I make it a habit to perform a typedef statement, or just take advantage of the C++ compiler feature?

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 GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 04:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.