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!

gettimeofday

Status
Not open for further replies.

dragosgcc

Programmer
May 7, 2006
1
US
I have the following code:

tv->tv_sec = malloc(sizeof(time_t));
tv->tv_usec = malloc(sizeof(suseconds_t));
if (gettimeofday(tv, tz) < 0) {
printf("Error getting time of day!\n");
exit(0);
}
printf("%f \n", &tv->tv_sec);
printf("%f \n", &tv->tv_usec);

At compile time I get the following error:
timegettimeofday.c:20: warning: assignment makes integer from pointer without a cast
timegettimeofday.c:21: warning: assignment makes integer from pointer without a cast

And if I run it, the results are:
-1.581093
-1.581093

Could you help me fix this please? What am I doing wrong?
 
I don't know what the timeval struct looks like, and since you didn't specify where lines 20 & 21 are, I'm assuming it's referring to the two printf() lines. Try removing the '&' characters or replace them with '*' characters.
Code:
printf("%f \n", tv->tv_sec);
or
Code:
printf("%f \n", *tv->tv_sec);
 
You have too many levels of indirection.

Code:
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);  /* use & here */
printf("%ld\n", tv.tv_sec);  /* members are long, so format is %ld */

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top