首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C中的struct timeval存储大小未知

C中的struct timeval存储大小未知
EN

Stack Overflow用户
提问于 2021-01-29 06:02:40
回答 1查看 687关注 0票数 2

嗨,我需要帮助解决这个错误在下面的C代码。错误是

代码语言:javascript
运行
复制
error: storage size of 't0' isn't known         
error: storage size of 't1' isn't known

我包括以下内容

代码语言:javascript
运行
复制
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
代码语言:javascript
运行
复制
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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-29 06:12:12

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

代码语言:javascript
运行
复制
 #include <sys/time.h>   //you don't include this header

时间h:

代码语言:javascript
运行
复制
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
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65949537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档