【问题标题】:Storage size unknown for struct timeval in C [duplicate]C中struct timeval的存储大小未知[重复]
【发布时间】:2021-05-03 01:57:27
【问题描述】:

您好,我需要帮助解决以下 C 代码中的此错误。错误是

error: storage size of 't0' isn't known         
error: storage size of 't1' isn't known

我包括以下内容

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void timer ( void (*f) (), char letter){ //function used to calculate average runtime
  int i = 0;


  struct timeval t0;
  struct timeval t1;
  gettimeofday(&t0,0);//takes start time
  for (i = 0; i < 100; i++){
    f(); // runs function
  }
  gettimeofday(&t1,0); //takes end time

  long seconds =  t1.tv_sec - t0.tv_sec;
  long  microseconds = (t1.tv_usec - t0.tv_usec);
  seconds = (1000000)*seconds + microseconds;
//  printf("Total time elapsed for workload %c: %d in microseconds\n", letter, seconds );
  printf("Average time elapsed for workload %c: %d %d/%d in microseconds\n", letter, seconds/100, seconds%100, 100 );//prints the average time as a whole number and then a fraction
}

【问题讨论】:

  • 范围内没有struct timeval 的定义。你需要#include &lt;sys/time.h&gt;

标签: c struct time


【解决方案1】:

struct timeval 的定义存在于 time.h 中,因此您必须包含此标头。

 #include <sys/time.h>   //you don't include this header

时间.h:

struct timeval {
   time_t      tv_sec;   // Number of whole seconds of elapsed time
   long int    tv_usec;  // Number of microseconds of rest of elapsed time minus tv_sec. Always less than one million
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2010-12-11
    • 2012-02-06
    • 1970-01-01
    • 2017-07-24
    相关资源
    最近更新 更多