C imlicit declaration of a function

↘锁芯ラ 提交于 2019-12-25 16:08:30

问题


I am on Linux and gcc 4.2.3.

For the below code portion, lp_parm_talloc_string function is called implicitly and afterwards it is defined:

char *lp_parm_string(const char *servicename, const char *type, const char *option)
{
        return lp_parm_talloc_string(lp_servicenumber(servicename), type, option, NULL);
}

/* Return parametric option from a given service. Type is a part of option before ':' */
/* Parametric option has following syntax: 'Type: option = value' */
/* the returned value is talloced in lp_talloc */
char *lp_parm_talloc_string(int snum, const char *type, const char *option, const char *def)
{
        param_opt_struct *data = get_parametrics(snum, type, option);

        if (data == NULL||data->value==NULL) {
                if (def) {
                        return lp_string(def);
                } else {
                        return NULL;
                }
        }

        return lp_string(data->value);
}

For this portion the below error comes up:

param/loadparm.c:2236: error: conflicting types for 'lp_parm_talloc_string'
param/loadparm.c:2229: error: previous implicit declaration of 'lp_parm_talloc_string' was here

How to tell compiler to allow such this case?


回答1:


You need to declare your function before using it:

char *lp_parm_talloc_string(int snum, const char *type, const char *option, const char *def);

char *lp_parm_string(const char *servicename, const char *type, const char *option)
{
    return lp_parm_talloc_string(lp_servicenumber(servicename), type, option, NULL);
}

// ...and the rest of your code

Or simply change the order the two functions appear in your source.



来源:https://stackoverflow.com/questions/5272514/c-imlicit-declaration-of-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!