Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:29:21+00:00 2026-05-27T23:29:21+00:00

I am messing around with the time header and have encountered an error: invalid

  • 0

I am messing around with the time header and have encountered an error:

invalid conversion from 'tm*' to 'time_t'

You can see the where the error is in the code below all the way at the bottom when I try to use the difftime function. I know im just doing something stupid and illegal but I cant see how I can get around this. If anyone has a solution let me know. Thanks!

The program is supposed to get the current time, take time input from a user, and then find the time difference.

Code:

#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  struct tm * cur_timeinfo_hold;
  int year, month ,day,hour,minute,second;
  double dif;

  /* prompt user for time to be compared*/
  printf ("Enter year: "); scanf ("%d",&year);
  printf ("Enter month: "); scanf ("%d",&month);
  printf ("Enter day: "); scanf ("%d",&day);
  printf ("Enter hour: "); scanf ("%d",&hour);
  printf ("Enter minute: "); scanf ("%d",&minute);
  printf ("Enter second: "); scanf ("%d",&second);

  /* get current timeinfo*/
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  /* print it */
  printf("The present time is: "); printf(asctime (timeinfo));

 /* set current time into a new variable to use for difftime, since timeinfo, will be changed */
  cur_timeinfo_hold = timeinfo;

  /* modify current timeinfo to the user's choice */
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;
  timeinfo->tm_hour = hour;
  timeinfo->tm_min = minute;
  timeinfo->tm_sec = second;
  mktime ( timeinfo );
  /* and print it */
  printf ("time to compare: "); printf(asctime (timeinfo));

  /* find time difference */
 //dif = difftime (cur_timeinfo_hold,timeinfo); //error: invalid conversion from 'tm*' to 'time_t'

  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:29:22+00:00Added an answer on May 27, 2026 at 11:29 pm

    mktime is a function. It accepts one parameter, a tm pointer, and returns a value, a time_t. It does not transform its argument from one type into another. Therefore, after you call mktime(timeinfo), your timeinfo variable is still a tm pointer.

    The difftime function expects to receive two time_t values, so it won’t accept cur_timeinfo_hold or timeinfo; they’re the wrong type. The first variable’s value came from converting rawtime to a tm pointer with localtime, so use that for the first parameter. When you later called mktime(timeinfo), it returned a time_t value, but you ignored the return value. Assign the return value to a variable so you can use it for the second difftime parameter.

    time_t info = mktime(timeinfo);
    // ...
    dif = difftime(rawtime, info);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The situation is that I've spent some time messing around with some experimental code.
I am messing around with assembly for the first time, and can't seem to
I'm messing around with stored procedures for the first time, but can't even create
I've spent a decent amount of time on the web and messing around in
I am messing around with Doctrine (version 1.0.3) to see if would make a
I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00
I'm messing around with SQLite for the first time by working through some of
I'm messing around with GTK and glade for the first time, and I've run
I have started messing around with the MVVP pattern, and I am having some
I've just been messing around with file_get_contents() at school and have noticed, it allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.