Pergunta

Compiling with gcc 4.4.3 on Ubuntu 10.04.2 x86_64 I get the following warning:

warning: comparison between pointer and integer

for this line:

if (strptime(date_time, "%d-%b-%y %T", &tm) == NULL) {

If I change NULL to 0 the warning goes away. But the man page for strptime states that it returns NULL on error. I am including <time.h> with #define __USE_XOPEN 1 on the previous line. I have also tried #define _XOPEN_SOURCE.

Thank you for your time.

EDIT

The full includes:

#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"

EDIT

The following code gives the same warning:

#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;
}

EDIT EDIT

But changing it to _XOPEN_SOURCE worked! And moving the define to the top of the program fixed the original.

Foi útil?

Solução

[edited after posting of complete includes-block]

You're using the wrong feature-selection macro and you're doing it in the wrong place.
#define __USE_XOPEN 1 only works when glibc does it internally, not when you do it.
#define _XOPEN_SOURCE is what you're supposed to use, but it only works if you put it before all #includes of system headers.

Also, your code shows poor style: explicit comparison to NULL (or 0) inside an if is a bad code smell. You should write it like this:

if (!strptime(...))

Also also, reasonable people can disagree with this, but I don't believe in using NULL at all. In C, 0 is a perfectly good null pointer constant except under very unusual conditions -- and under those conditions NULL doesn't work either. (Things are different in C++.)

Outras dicas

According to POSIX documentation, strptime is declared in <time.h>.

You need

#define _XOPEN_SOURCE
/* other headers, if needed, after the #define
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
*/
#include <time.h>

to have a correct prototype in scope.

Without a prototype, the compiler assumes functions return a int.

I suppose you are getting that warning because strptime isn't declared. (Without a declaration, strptime defaults to returning an int.) As you have already guessed, this is probably due to a missing #define _XOPEN_SOURCE.

The following program produces no warnings, using "gcc" on Ubuntu 10.04.2 LTS. Is this what your program looks like?

#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;
}

EDIT You must not define __USE_XOPEN. You must define _XOPEN_SOURCE. From the linux man page, the correct usage is :

#define _XOPEN_SOURCE
#include <time.h>

Simple. compare it with 0. if strptime(date_time, "%d-%b-%y %T", &tm) == 0

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top