[英]c strptime warning comparison between pointer and integer
在 Ubuntu 10.04.2 x86_64 上使用 gcc 4.4.3 進行編譯,我收到以下警告:
warning: comparison between pointer and integer
對於這一行:
if (strptime(date_time, "%d-%b-%y %T", &tm) == NULL) {
如果我將 NULL 更改為 0,警告就會消失。 但是 strptime 的手冊頁指出它在錯誤時返回 NULL。 我在前一行包含了<time.h>
和#define __USE_XOPEN 1
。 我也試過#define _XOPEN_SOURCE
。
感謝您的時間。
編輯
完整的包括:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define __USE_XOPEN 1 /* needed for strptime */
#include <time.h>
#include <arpa/inet.h>
#include <errno.h>
#include "recv.h"
#include "tcp.h"
#include "types.h"
編輯
以下代碼給出了相同的警告:
#define __USE_XOPEN 1 /* needed for strptime */
#include <time.h>
#include <stdio.h>
int main()
{
struct tm tm;
char date_time[] = "3-May-11 12:49:00";
if (strptime(date_time, "%d-%b-%y %T", &tm) == NULL) {
fprintf(stderr, "Error: strptime failed matching input\n");
}
return 0;
}
編輯編輯
但是將其更改為 _XOPEN_SOURCE 有效。 並將定義移動到程序頂部修復了原始文件。
根據POSIX 文檔, strptime
在<time.h>
中聲明。
你需要
#define _XOPEN_SOURCE
/* other headers, if needed, after the #define
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
*/
#include <time.h>
在 scope 中有一個正確的原型。
如果沒有原型,編譯器會假定函數返回一個int
。
[在發布完整包含塊后編輯]
您使用了錯誤的功能選擇宏,並且您在錯誤的位置進行操作。
#define __USE_XOPEN 1
僅在 glibc 在內部執行時有效,而不是在您執行時。
#define _XOPEN_SOURCE
是你應該使用的,但它只有在你把它放在系統頭的所有#include
之前才有效。
此外,您的代碼顯示出糟糕的風格:與if
內的 NULL(或 0)的顯式比較是不好的代碼氣味。 你應該這樣寫:
if (!strptime(...))
此外,理性的人可能不同意這一點,但我根本不相信使用 NULL。 在 C 中,0 是一個非常好的 null 指針常量,除非在非常不尋常的條件下——並且在這些條件下NULL 也不起作用。 (C++ 中的情況有所不同。)
我想您收到該警告是因為未聲明strptime
。 (沒有聲明, strptime
默認返回一個int
。)正如您已經猜到的,這可能是由於缺少#define _XOPEN_SOURCE
。
以下程序在 Ubuntu 10.04.2 LTS 上使用“gcc”不產生任何警告。 這是你的程序的樣子嗎?
#define _XOPEN_SOURCE
#include <time.h>
int main() {
struct tm tm;
char date_time[] = "Monday morning";
if (strptime(date_time, "%d-%b-%y %T", &tm) == NULL) {
}
return 0;
}
編輯您不能定義 __USE_XOPEN。 您必須定義 _XOPEN_SOURCE。 從 linux 手冊頁中,正確的用法是:
#define _XOPEN_SOURCE
#include <time.h>
簡單的。 將其與 0 進行比較。 if strptime(date_time, "%d-%b-%y %T", &tm) == 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.