【问题标题】:C - Improper Pointer/Integer Combination in strftime()C - strftime() 中不正确的指针/整数组合
【发布时间】:2010-10-30 14:08:43
【问题描述】:

我收到编译器错误:(83) 错误:不正确的指针/整数组合:arg #1。

这是执行此操作的代码:

char boot_time[BUFSIZ];

... 第 83 行:

strftime(boot_time, sizeof(boot_time), "%b %e %H:%M", localtime(table[0].time)); 

其中 table 是一个结构,time 是一个 time_t 成员。

我读到“不正确的指针/整数组合”意味着函数未定义(因为在 C 中,函数在找不到时返回整数),通常的解决方案是包含一些库。 strftime() 和 localtime() 都在 time.h 中,而 sizeof() 在 string.h 中,我已经包含了这两个(连同 stdio.h)我在这里完全被难住了。

【问题讨论】:

  • 吹毛求疵:sizeof 不是函数。它是一个 C 内置函数(关键字),并且没有在任何标题中定义。您也不需要括号,除非它们是参数的一部分(对于类型)。在本例中,只需写“sizeof boot_time”,因为您不需要类型的大小。

标签: c strftime


【解决方案1】:
struct tm * localtime ( const time_t * timer );

正确的usage 是:

time_t rawtime;
localtime(&rawtime);

在你的情况下:localtime(&(table[0].time))

【讨论】:

    【解决方案2】:

    localtime 接受time_t*,因此传递&table[0].time(地址,而不是值)。

    【讨论】:

      【解决方案3】:

      问题似乎是对本地时间的调用。这个函数需要一个time_t 指针而不是一个值。我相信您需要按以下方式拨打电话

      localtime(&(table[0].time))
      

      当地时间的签名

      struct tm * localtime ( const time_t * timer );
      

      对本地时间 API 的引用

      【讨论】:

        【解决方案4】:

        正如其他人所提到的,特定的问题是您需要将 time_t * 传递给本地时间。

        但是,普遍的问题是您在执行多项操作的复杂线路上遇到了一个不清楚的问题。当您遇到错误时,首先要尝试的是将线路拆分为其组成部分,以缩小问题的确切位置,如下所示:

        char boot_time[BUFSIZ];
        // Temporary, putting the sizeof() call inline is normally better.
        size_t boot_time_size = sizeof(boot_time); 
        time_t temp_time = table[0].time;
        // Use a more descriptive name here.
        struct tm *mytime = localtime(temp_time); 
        
        strftime(boot_time, boot_time_size, "%b %e %H:%M", mytime);
        

        通过这种方式,编译器可以告诉您哪个调用实际上给您带来了问题。一旦你弄清楚了,你可以在你认为合适的时候把它压缩回去——我可能仍然会把 localtime() 调用保留在自己的线路上,但这只是我。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-16
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          相关资源
          最近更新 更多