Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hey guys, I think I might be lookin

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
Hey guys, I think I've been looking at this code for too long, but I have a simple question for you. ok, I have a pre-defined structure I want to pass into a subroutine. I need to fill the structure in the sub, then use it outside the sub afterwards. How would I go about doing it in the right way. Here's what I have:

struct TokenHeader strttimestamp;

sub()
{
...
...

if (!(GetTimeStamp(strttimestamp)))
return(1);
...
...
}

static int GetTimeStamp(strttimestamp)
struct TokenHeader *strttimestamp;
{
FILE *strttimestamp_fp;

if(!(strttimestamp_fp = fopen(timestamp_path, "rb")))
{
LogIt(1,FLAGIT,"%s: Unable to read %s file.", process_name, timestamp_path);
return(0);
}

if(fread((char *)&strttimestamp, 1, 12, strttimestamp_fp) <= 0)
{
LogIt(1,FLAGIT,&quot;Can not read file %s - %s&quot;, timestamp_path, strerror(errno));
fclose(strttimestamp_fp);
return(1);
}
fclose(strttimestamp_fp);

return(0);
}


Thanks
M
 
The way to do this is to pass the address of the structure to the function along the following lines.


struct _test
{
int a;
int b;
};

void foobar(struct _test *);

main()
{
struct _test mytest;

foobar(&mytest);
printf(&quot;Results = %d and %d\n&quot;,mytest.a, mytest.b);
return;
}

void foobar(struct _test p_mytest)
{
p_mytest->a=1;
p_mytest->b=2;
}


Hope this gives you the idea.

Cheers - Gavin
 
Soory - type in my last post, the function should read

void foobar(struct _test * p_mytest)
{
p_mytest->a=1;
p_mytest->b=2;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top