• C

Not sure how to properly set up data types.

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;
}
ineedzzz8Asked:
Who is Participating?
 
efnCommented:
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
 
leisnerCommented:
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
 
ineedzzz8Author Commented:
ah that makes sense.  Thanks guys.
0
 
sunnycoderCommented:
>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
 
sunnycoderCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.