?
Solved

Not sure how to properly set up data types.

Posted on 2006-03-25
5
Medium Priority
?
215 Views
Last Modified: 2010-04-15
I am trying to teach myself C and build a little golf program.  I am having problems with how to set up data types with multiple fields.  If I set up a data type AMATEURS with a handful of fields in it.  Then I want to set up another one TOURNEY and have it contain tournament stats as well as an array of amateurs.

Anyways ... back to my question... can I initialize the tourney data type and set it to return type tourney?  or is there an object tag I should be using.
typedef struct {
      int key;
} AMATEUR;

typedef struct {
      int capacity;
      int size;
      AMATEUR H[400];            //how do i set the max to the tourney capacity?
} TOURNEY;

TOURNEY Initialize(int n);
{
      TOURNEY.capacity = n;
      TOURNEY.size = 0;
      return TOURNEY;
}
0
Comment
Question by:ineedzzz8
5 Comments
 
LVL 3

Assisted Solution

by:leisner
leisner earned 140 total points
ID: 16291873
Your not defining storage, just the basic types....

YOu need to do something like


void Initlialize(int n, TOURNEY tournament )
{
    tournament.capacity = n;
    tournament.size = 0;
}

You have to be concerned about where storage is, you have to understand the stack and the heap....

0
 
LVL 15

Accepted Solution

by:
efn earned 160 total points
ID: 16291944
With your typedef statement, you have defined a type called TOURNEY.  If you want to do something with a TOURNEY, you have to create an object of that type.  The simplest way is just to declare one.  For example:

TOURNEY masters;

This declares one TOURNEY named "masters".  Then you can initialize it.  For example:

masters.capacity =27;

In order to have a function create or initialize the TOURNEY, you would have to get into pointers or global data.  Unless you have some specific need for a function, it will be simplest just to declare the object, as shown above.
0
 

Author Comment

by:ineedzzz8
ID: 16291967
ah that makes sense.  Thanks guys.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16291969
>can I initialize the tourney data type
Yes ...

>and set it to return type tourney?  
If you meant return type of function, then yes you can set it to your typedef'd data type TOURNEY .. no problems

>or is there an object tag I should be using.
Never heard that ... what is an object tag?

As leisner pointed out .. you have not declared any variable so far .. all you have done is declared datatype ...

Another point, when there is an array in the struct, it is best not to return struct but a pointer to struct. Copying entire start variable on stakc comsumes space as well as time. A refernce woudl work much better ... e.g.

typedef struct {
     int key;
} AMATEUR;

typedef struct {
     int capacity;
     int size;
     AMATEUR H[400];          //how do i set the max to the tourney capacity? -------> What do you mean by max??
} TOURNEY;

void Initialize(int n, TOURNEY * tournament);

int main ()
{
   TOURNEY  american_open;
   ....
   Initialize (125, &american_open);

}
void Initialize(int n, TOURNEY * tournament) //---->>>>> note no trailing ; here
{
     TOURNEY->capacity = n; //--------> since we are passing a pointer, we access members using -> and not .
     TOURNEY->size = 0;       //---->> also since we have access to exact same memory locations, we do not have to return anything
}

Cheers!
sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16292000
an oops in the code above
     TOURNEY->capacity = n;
     TOURNEY->size = 0;  

Should have been

     tournament->capacity = n;
     tournament->size = 0;  
Use the variable name and not type ...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question