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

  2. Flash Gordon Guest

    wrote:
    >
    > 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"


    Please don't use tabs when posting to Usenet it can cause all sorts of
    problems. I will converts tabs to spaces in the rest.

    Also, please don't repost within a few hours, you need to leave a couple
    of days before assuming people are not going to respond, *especially*
    when posting on a Sunday afternoon!

    > #include <stdlib.h>
    >
    >
    > main(argc, argv)
    > int argc;
    > char **argv;


    This, whilst still legal, is the old style declaration. Unlike the new
    style it does not help the compiler diagnose errors. I strongly suggest
    you use the new (in 1989) prototype style of declaration:
    int main(int argc, char **argv)

    > {
    >
    > int GetCurrentTime();


    Yuck. It is generally better style to provide declaration at file scope,
    not block scope. Also, provide a prototype so the compiler will validate
    the parameter passing.

    > struct tm *time_struct;
    >
    >
    > GetCurrentTime(time_struct);


    Above you are evaluated an uninitialised variable (which happens to be a
    pointer) *BANG*.

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


    Again, use the prototype form.

    <snip>

    > The "include.h" just has stdio.h, time.h and sys/systypes.h as further
    > includes.


    In my opinion it is bad style to hide required includes within an
    include file. Each file should directly include all the headers it
    depends on so you can see that everything required is included without
    having to dig. sys/systypes.h is a non-standard header and we know
    nothing about it here, although that is not your problem.

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


    No, variable and &variable are *never* the same thing. Sometimes they
    differ only in type (when dealing with arrays) but they are always
    different.

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


    You obviously missed http://c-faq.com/ptrs/passptrinit.html which is
    your problem (the fact you are dealing with a struct is irrelevant).
    I suggest you read the rest of section 4 as well, and read section 6
    before you start using arrays.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 9, 2006
    #2
    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:
    325
    Martin Ambuhl
    Jan 9, 2006
  2. Replies:
    0
    Views:
    277
  3. Casey Hawthorne
    Replies:
    1
    Views:
    745
    Arne Vajhøj
    Mar 18, 2009
  4. Replies:
    0
    Views:
    329
  5. Mark
    Replies:
    2
    Views:
    412
Loading...

Share This Page