By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

Passing Struct to Function to be Modified then used by Caller

P: n/a
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 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<jc******@rochester.rr.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com...
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;
}
Jan 9 '06 #2

P: n/a
jc******@rochester.rr.com 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:
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...

Jan 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.