Taking structs to the next level

This is a discussion on Taking structs to the next level within the C Programming forums, part of the General Programming Boards category; Hey me again. Ok for my gym program, i've got a struct like this Code: typedef struct weights{ int reps; ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    Taking structs to the next level

    Hey me again.

    Ok for my gym program, i've got a struct like this
    Code:
    typedef struct weights{
                 int reps;
                 int sets;
                 int weight;
                 }exercises;

    this is good because i can call it as follows exercises benchpress.reps = 5;

    Now suppose i want to take this one step further and have a struct which holds the information for a weights routine as opposed to just a single exercise. How do i go about this. Each routine would have a different number of exercises. Im not sure how to add this other level to stucts.

    I'd like to have a struct of routines, that i could call with

    routines.chestAndTriceps

    in this sub heading if you like would be a list of mabye 7 exercises all of wich ideally are individual members of the exercises struct.

    I dont even really know if its possible and im not 100% sure of what im asking for or how to put across what im looking.

    But if my some miracle you understand, id appreciate a push in the right direction

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect what you really want is a struct like this:
    Code:
    typedef struct weights{
                 int reps;
                 int sets;
                 int weight;
                 char *name;
                 }exercises;
    
    // Then:
    excercises routines[] = 
    {
        { 0, 0, 0, "Benchpress" },
        { 0, 0, 0, "ChestAndTriceps" },
    ...
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My Hat of Guessing gives you the following code to look at:
    Code:
    typedef struct {
        int reps;
        int sets;
        int weight;
    } exercises;
    
    typedef struct {
        int numOfExercises;
        exercises *exerciseList; /* to be interpreted later as an array */
    } routines;
    
    .
    .
    .
    routines chestAndTriceps;
    chestAndTriceps.numOfExercises = 7;
    chestAndTriceps.exerciseList = malloc(sizeof(exercises)*7); /* allocate memory for "array" */
    The "variable-size array" might be a bit more complex than you want, but that should give you a shove (whether in the right direction or in the mud is yet to be determined).

    Edit to add: and you would then access elements of your exercises as
    Code:
    chestAndTriceps.exerciseList[0].reps /*et cetera*/

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Thanks guys. Tabstop, i think thats exactly what i need.

    Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Diablo's random level generation
    By glo in forum Game Programming
    Replies: 7
    Last Post: 07-19-2008, 03:04 AM
  2. Listing binary tree numbers by level
    By Nazgulled in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 10:36 AM
  3. level up 6 times with 40 EXP!?!?!?!
    By Blizzarddog in forum Game Programming
    Replies: 15
    Last Post: 03-05-2003, 11:56 AM
  4. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21