473,574 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_check s 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_valu e);
ts_ptr = localtime(&time _value);
fprintf(stdout, "**\tGetCurrent Time %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 2505

<jc******@roche ster.rr.com> wrote in message
news:11******** ************@o1 3g2000cwo.googl egroups.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_check s 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_valu e);
ts_ptr = localtime(&time _value);
fprintf(stdout, "**\tGetCurrent Time %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_check s 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_valu e);

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

if((ts_ptr = localtime(&time _value)) != NULL)
{
fprintf(stdout, "**\tGetCurrent Time %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******@roches ter.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_check s 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_valu e);
memcpy(ts_ptr, localtime(&time _value), sizeof *ts_ptr);
fprintf(stdout, "%s\n", ctime(&time_val ue));
}
[Output]
Mon Jan 9 11:48:57 2006

Starting get_error_check s 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_check s 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_valu e);
ts_ptr = localtime(&time _value);
fprintf(stdout, "**\tGetCurrent Time %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
34353
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would...
4
3611
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
6
12652
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
1
358
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> main(argc, argv) int argc;
5
3322
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 of the function, but I have only the following horrific way to do that. Can someone please suggest a better alternative? #include <stdio.h> ...
50
6441
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 union's types can be passed to a function directly -- without creating a separate variable of the union type and assigning the appropriate field of...
5
2514
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. Typically I have a helper function that does common things like replace all occurances of a STL string in a given STL string. These strings can be...
6
8957
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 must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of...
13
1683
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 core dumps I can see that sometimes in function calls, the caller passes a value and the callee receives another value... Usually it seems to be a...
160
5787
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
7738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7828
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6476
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5316
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3755
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3765
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.