Passing Struct to Function to be Modified then used by Caller

J

jconnort

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

pemo

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

There were a few things wrong. Like your original pointer didn't point to
anything. This seems to work though [below] - if there's any further
questions about it, fire away.

struct tm * GetCurrentTime(void);

int main(int argc, char ** argv)
{
struct tm * time_struct;

time_struct = GetCurrentTime();

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

getchar();

exit(0);
}



struct tm * GetCurrentTime(void)
{
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"};

struct tm * ts_ptr = NULL;

time_t time_value=0;

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

time(&time_value);

/* localtime() returns a pointer to static storage.
*/

if((ts_ptr = localtime(&time_value)) != NULL)
{
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);
}

/* although ts_ptr is a 'local' variable, returning its 'value' is ok as
localtime()
** returns a pointer to static storage. If localtime fails, the value
is NULL.
*/
return ts_ptr;
}
 
M

Martin Ambuhl

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:

The OP's code is retained at EOM. Try the following, noting the
differences between it and your code.

#include <stdio.h>
#include <time.h>
#include <string.h>

void GetCurrentTime();

int main(void)
{

struct tm time_struct; /* NOTE: not a pointer */
GetCurrentTime(&time_struct);
fprintf(stdout, "Starting get_error_checks run on: %d...\n\n",
time_struct.tm_year + 1900);
return 0;
}

void GetCurrentTime(struct tm *ts_ptr)
{
time_t time_value = 0;
time(&time_value);
memcpy(ts_ptr, localtime(&time_value), sizeof *ts_ptr);
fprintf(stdout, "%s\n", ctime(&time_value));
}


[Output]
Mon Jan 9 11:48:57 2006

Starting get_error_checks run on: 2006...


[OP's code]

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top