Thread: Passing Structure Pointers

  1. #1
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Unhappy Passing Structure Pointers

    Hello,
    I need to create and pass 2 structure pointers to a function. The function will take the pointers and subtract them.

    This is a time program, so users enter the 24 hour clock time, twice and subtract them.

    My first structure looks like this:
    struct time_of_day
    {
    short hour[24];
    short minute[60];
    };

    void settime();

    Not sure how I should attack this



    Thanks
    Matt

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    If you know how to work with the function, your parameter passing would be something like this:
    Code:
    void settime(time_of_day *first_day, time_of_day *sec_day)
    {
    /* CODE */
    }
    Is that your question? Or do you need some pointers (no pun intended) on how to work with the subtraction?

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks for quick reply...first of all. I do have this in code :void settime(struct time_of_day) and i tried passing just the hours down without pointers and output is screwed up...Code below:


    #include <stdio.h>

    struct time_of_day
    {
    short hour[24];
    short minute[60];
    }rec;

    void settime(struct time_of_day time);

    int main ()
    {


    //struct time_of_day firsttime;
    //struct time_of_day secondtime;

    // printf("Enter first time, hour: ");
    // scanf("%d", firsttime.hour);

    printf("Please enter the hour: ");
    scanf("%d", &rec.hour );

    settime(rec);

    }

    void settime(struct time_of_day time)

    {

    printf("The time is: %d",time.hour );



    }

    Guess im "C" sick...not enough reps on the structure examples either.

    Any ideas?
    Thanks again,
    Matt

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I think your problem is in the struct. Why is the hour member an array of 24? Do you that? If you do, then you'll have to specify the output of the array. For instance:
    Code:
    // changed struct
    struct time_of_day 
    { 
    short hour; 
    short minute; 
    }rec;
    Try this. You don't want an array. And if you do, then you'll have to specify the element in the other functions. Tell me if you want to use the array. It doesn't (from this viewpoint) look neccessary to.

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    I need a function that accepts two time_of_day structure pointers
    and prints the time difference between them (i.e. if one structure
    represented 13:30 and the other 23:15 the function would
    print "09:45". Assume that both times are from the same 24 hour day.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Did you fix the output problem? Try out the function and show me what you have and I'll take a look at it.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    are you still there? I have been working on it since your last post...and had it almost working once. Must be tired, as I cannot get the function working.



    #include <stdio.h>

    struct time_of_day
    {
    short hour[24];
    short minute[60];
    }rec;

    void settime(struct time_of_day *first_day);


    struct time_of_day *first_day;

    int main ()
    {
    fflush(stdin);
    printf("Please enter the time for first day: ");
    scanf("%d%d",&first_day);


    settime(first_day);

    }

    void settime(struct time_of_day *first_day)



    {
    // short y;


    printf("The first day time is: %d\n%d\n", first_day);
    //printf("The second day time is %d\n", second_day);

    //x=(first_day - second_day);

    //printf("The Difference between first and second day time is: %d\n", x);


    }


    I technically only need to get define a function (for school) that can accept 2 time of day structure pointers and print out the difference between them. However, I would like to actually learn how to do it. Having trouble getting it all to work.

    Matt

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > printf("The first day time is: %d\n%d\n", first_day);

    This is your problem. You cannot just print the entire structure like this.

    You must print its elements individually by their "base" type (such as int, char, etc).

    To do this, you have to access the variable in the structure.
    This is how it works:

    When accessing directly, use "."
    When accessing via pointer, use "->"

    Therefore:
    Code:
    struct mystruct {
        int x;
    };
    
    void someFunction( struct mystruct m )
    {  /*passed by value */
        printf("m.x == %d", m.x );
    }
    
    void someFun2( struct mystruct *m )
    {  /* passed by reference */
        printf("m->x == %d", m->x );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Sayeh
    Guest
    Ummmm, this is like _so incredibly simple_...


    struct time_of_day
    {
    short hour[24];
    short minute[60];
    }rec;


    First, don't use arrays-- you're thinking of this like a string. Instead, try this-- helps readability alot and is the correct way to define a new type.

    /* typedefs */

    typedef struct
    {
    int hour;
    int minute;
    }rec;


    /* prototypes */

    int main(void);
    void diftime(rec*,rec*); /* get difference in times */


    /* functions & procs */

    /*****
    *
    * MAIN - what's to say, it's main
    *
    *****/

    int main(void)
    {
    rec timeA; /* our local vars */
    rec timeB;

    timeA.hour = 12; /* init vars */
    timeA.minute = 15;

    timeB.hour = 17;
    timeB.minute = 30;

    diftime(&timeA,&timeB);

    printf("Difference Time is: %i:%i",timeA.hour,timeA.minute);
    }

    /*****
    *
    * DIFTIME - returns 'difference' between two times
    *
    *****
    *
    * NOTE-- this function destroys info in first argument passed
    *
    *****/

    void diftime(rec *a,rec* b)
    {
    a.hour = abs(a.hour - b.hour);
    a.minute = abs(a.minute - b.minute);
    };

    -----------------

    anyway, something like that.

    enjoy.

  10. #10
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks, that helped. However, I get errors when I run this. Is my pointer okay? Also, I want the time to come out as say: "23:14" i.e. 23 hours 14 minutes how can I do this?
    Thanks again:
    #include <stdio.h>

    struct time_of_day
    {
    short hour[24];
    short minute[60];
    }rec;

    void settime(struct time_of_day *first_day);


    struct time_of_day *first_day;

    int main ()
    {
    fflush(stdin);
    printf("Please enter the time for first hour: ");
    scanf("%d",&first_day->hour);

    fflush(stdin);
    printf("Please enter the time for first hour: ");
    scanf("%d",&first_day->minute );

    settime(first_day);

    }

    void settime(struct time_of_day *first_day)



    {

    printf("The first day time is: %d%d\n",first_day->hour, first_day->minute );

  11. #11
    Sayeh
    Guest

    correction

    Sorry, my diftime() procedure should have been--

    void diftime(rec *a,rec* b)
    {
    a->hour = abs(a->hour - b->hour);
    a->minute = abs(a->minute - b->minute);
    };

  12. #12
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Thanks much appreciated. Now, I am a newbie to C (obviously) and some of this is all new to me. Now do I have to use typedef struct or could I just use struct?

  13. #13
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Sorry to leave so early but it was past my bed time (here in NJ).

    >> Now do I have to use typedef struct or could I just use struct? <<
    You can just use struct. typedef makes it a little easier, but it is absolutely not necessary.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure to function
    By Sereby in forum C Programming
    Replies: 4
    Last Post: 07-28-2004, 10:05 PM
  2. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM
  3. Passing Pointers By Reference
    By hern in forum C Programming
    Replies: 15
    Last Post: 07-29-2003, 11:43 AM
  4. passing struct pointers to make tables/files
    By Jarrette in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2003, 06:23 PM
  5. global structure pointers
    By threahdead in forum Linux Programming
    Replies: 2
    Last Post: 03-13-2003, 12:21 PM