473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing Struct to Function to be Modified then used by Caller

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
2 2477

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
1
by: jconnort | last post by:
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> ...
5
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
5
by: Mike Cain | last post by:
Hi - I am looking for the most efficient way to pass a STL string from one function to another (using MS VS 7.0 ATL if that matters) and have a few questions abuot the principles at work here. ...
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
13
by: frakie | last post by:
Hi 'body, I'm experiencing difficulties on parameter passing. I wrote a library in last months, and now it crashes few times in a month because of errors in parameter passing: using gdb on the...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.