Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing Struct to Function to be Modified then used by Caller

Reply
Thread Tools

Passing Struct to Function to be Modified then used by Caller

 
 
jconnort@rochester.rr.com
Guest
Posts: n/a
 
      01-08-2006


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

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Struct to Function to be Modified then used by Caller jconnort@rochester.rr.com C Programming 2 01-09-2006 04:51 PM
Passing Struct to Function to be Modified then used by Caller jconnort@rochester.rr.com C Programming 0 01-08-2006 10:53 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
Can't get function caller if the caller is from a function within a popup window Mark Javascript 2 04-03-2004 07:57 AM



Advertisments