Passing Struct to Function to be Modified then used by Caller

Discussion in 'C Programming' started by jconnort@rochester.rr.com, Jan 8, 2006.

  1. Guest

    I'm trying to write a function to get the current system time, so I can
    use it when I need to output it to a log, below is the code I'm
    testing:

    #include "include.h"
    #include <stdlib.h>


    main(argc, argv)
    int argc;
    char **argv;
    {

    int GetCurrentTime();

    struct tm *time_struct;


    GetCurrentTime(time_struct);

    fprintf(stdout, "Starting get_error_checks run on: %d...\n\n",
    time_struct->tm_year+1900);

    exit(0);
    }



    int GetCurrentTime(ts_ptr)
    struct tm *ts_ptr;
    {
    static char *weekday[7] = {"Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday"};

    static char *month[12] = {"January", "February", "March",
    "April", "May", "June", "July", "August",
    "September", "October", "November", "December"};

    time_t time_value=0;

    /* Retrieve current time information from the Operating System . . .
    */

    time(&time_value);
    ts_ptr = localtime(&time_value);
    fprintf(stdout, "**\tGetCurrentTime %s, %s %d, %d
    %d:%02d\n**\n**\n*/\n\n\n",
    weekday[ts_ptr->tm_wday],
    month[ts_ptr->tm_mon],
    ts_ptr->tm_mday,
    ts_ptr->tm_year+1900,
    ts_ptr->tm_hour,
    ts_ptr->tm_min);
    return;

    }


    The "include.h" just has stdio.h, time.h and sys/systypes.h as further
    includes. This seems to work fine in the function call but I can't get
    the caller to "know" about the changes. I've tried declaring
    time_struct as just a pointer (as above) or having a struct declared,
    passing in time_struct, &time_struct (which I think are the same in
    this case ?) having a separate pointer to the struct get passed, with
    no luck. Obviously I'm missing something fundamental here, can anyone
    help ? I have been going through FAQs and some some similar issues...
     
    , Jan 8, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    320
    Martin Ambuhl
    Jan 9, 2006
  2. Replies:
    1
    Views:
    279
    Flash Gordon
    Jan 9, 2006
  3. Casey Hawthorne
    Replies:
    1
    Views:
    724
    Arne Vajhøj
    Mar 18, 2009
  4. Replies:
    0
    Views:
    304
  5. Mark
    Replies:
    2
    Views:
    400
Loading...

Share This Page