繁体   English   中英

指向函数的gcc警告(不兼容的指针类型)

[英]gcc warning in pointer to function (incompatible pointer type)

我需要gcc警告的帮助,看起来我在这里做错了

unsigned long convert_syslog(char *date,int year) {
    char withyear[1024];
    struct tm tm;
    time_t epoch = (time_t) NULL;

    snprintf(withyear,sizeof(withyear),"%s %i",date,year);
    if(strptime(withyear,"%b %d %H:%M:%S %Y",&tm)) {
            epoch = mktime(&tm);
            printf("%u\n",epoch);
    }
    return epoch;
}

unsigned long convert_tai64(char *date,int year) {
    char hex[16];
    unsigned long u;
    strcpy(hex,"0x");
    strcat(hex,strndup(date+8,8));
    return strtoul (hex,NULL,16);
}

unsigned long convert_nagios(char *date,int year) {
    return strtoul(date,NULL,10);
}

unsigned long convert_clf(char *date,int year) {
    struct tm tm;
    time_t epoch = (time_t)NULL;

    if(strptime(date,"%d/%b/%Y:%H:%M:%S",&tm)) {
            epoch = mktime(&tm);
    }
    return epoch;
 }




typedef unsigned long (*func)(char *data,int year);

func *convert_date(int pos) {
    switch(pos) {
            case 0: return &convert_syslog;
            case 1: return &convert_tai64;
            case 2: return &convert_clf;
            case 3: return &convert_nagios;
            default: return NULL;
    }
}

在convert_date中给我警告

pcre_search.c:57: warning: return from incompatible pointer type  (case 0)  
pcre_search.c:58: warning: return from incompatible pointer type   (...)
pcre_search.c:59: warning: return from incompatible pointer type   (...)
pcre_search.c:60: warning: return from incompatible pointer type   (default)

你应该返回func而不是func*

func typedef已经是一个指向函数的指针,因此convert_date可以像这样声明:

func convert_date(int pos) { ...

函数名(在C中)已经是指针,因此不需要在return语句中使用&。 这样做:

func convert_date(int pos) {
       switch(pos) {
           case 0: return convert_syslog;
           case 1: return convert_tai64;
           case 2: return convert_clf;
           case 3: return convert_nagios;
           default: return (func)NULL;
       }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM