• 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;

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

TOURNEY Initialize(int n);
      TOURNEY.capacity = n;
      TOURNEY.size = 0;
      return TOURNEY;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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....

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ineedzzz8Author Commented:
ah that makes sense.  Thanks guys.
>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;

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

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

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 ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.