Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将常量字符*复制到字符中**_C_Libconfig - Fatal编程技术网

C 将常量字符*复制到字符中**

C 将常量字符*复制到字符中**,c,libconfig,C,Libconfig,我正在使用libconfig库从文件中读取一些配置数据。我在提取用于解析信息并在事后清理的函数时遇到问题 运行strcpy(*hostname,tmp)会导致核心转储 主机名、端口和ip初始化为NULL int parseConfig(char **hostname, char **port, char **ip) { config_t cfg, *cf; const char *tmp; cf = &cfg; config_init(cf);

我正在使用
libconfig
库从文件中读取一些配置数据。我在提取用于解析信息并在事后清理的函数时遇到问题

运行strcpy(*hostname,tmp)会导致核心转储

主机名
端口
ip
初始化为
NULL

int parseConfig(char **hostname, char **port, char **ip) {

    config_t cfg, *cf;
    const char *tmp;

    cf = &cfg;
    config_init(cf);

    if(!config_read_file(cf, CONFIG)) {
        fprintf(stderr, "%s:%d - %s\n",
            config_error_file(cf),
            config_error_line(cf),
            config_error_text(cf));
        config_destroy(cf);
        return(EXIT_FAILURE);
    }

    config_lookup_string(cf, "hostname",  &tmp);
    strcpy(*hostname, tmp);
    config_lookup_string(cf, "ip", &tmp);
    strcpy(*ip, tmp);
    config_lookup_string(cf, "port", &tmp);
    strcpy(*port, tmp);

    config_destroy(cf);

    return(EXIT_SUCCESS);
}

因为它们被初始化为NULL,所以应该为它们分配足够的内存空间

config_lookup_string(cf, "hostname",  &tmp);
*hostname = malloc(strlen(tmp)+1);
strcpy(*hostname, tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = malloc(strlen(tmp)+1);
strcpy(*ip, tmp);
config_lookup_string(cf, "port", &tmp);
*port = malloc(strlen(tmp)+1);
strcpy(*port, tmp);
或者,如果您有可用的
strdup()

config_lookup_string(cf, "hostname",  &tmp);
*hostname = strdup(tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = strdup(tmp);
config_lookup_string(cf, "port", &tmp);
*port = strdup(tmp);

因为它们被初始化为NULL,所以应该为它们分配足够的内存空间

config_lookup_string(cf, "hostname",  &tmp);
*hostname = malloc(strlen(tmp)+1);
strcpy(*hostname, tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = malloc(strlen(tmp)+1);
strcpy(*ip, tmp);
config_lookup_string(cf, "port", &tmp);
*port = malloc(strlen(tmp)+1);
strcpy(*port, tmp);
或者,如果您有可用的
strdup()

config_lookup_string(cf, "hostname",  &tmp);
*hostname = strdup(tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = strdup(tmp);
config_lookup_string(cf, "port", &tmp);
*port = strdup(tmp);

因为它们被初始化为NULL,所以应该为它们分配足够的内存空间

config_lookup_string(cf, "hostname",  &tmp);
*hostname = malloc(strlen(tmp)+1);
strcpy(*hostname, tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = malloc(strlen(tmp)+1);
strcpy(*ip, tmp);
config_lookup_string(cf, "port", &tmp);
*port = malloc(strlen(tmp)+1);
strcpy(*port, tmp);
或者,如果您有可用的
strdup()

config_lookup_string(cf, "hostname",  &tmp);
*hostname = strdup(tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = strdup(tmp);
config_lookup_string(cf, "port", &tmp);
*port = strdup(tmp);

因为它们被初始化为NULL,所以应该为它们分配足够的内存空间

config_lookup_string(cf, "hostname",  &tmp);
*hostname = malloc(strlen(tmp)+1);
strcpy(*hostname, tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = malloc(strlen(tmp)+1);
strcpy(*ip, tmp);
config_lookup_string(cf, "port", &tmp);
*port = malloc(strlen(tmp)+1);
strcpy(*port, tmp);
或者,如果您有可用的
strdup()

config_lookup_string(cf, "hostname",  &tmp);
*hostname = strdup(tmp);
config_lookup_string(cf, "ip", &tmp);
*ip = strdup(tmp);
config_lookup_string(cf, "port", &tmp);
*port = strdup(tmp);
剪掉中间人

config_lookup_string(cf, "hostname",  hostname);
这要求您永远不要销毁
cfg
,因为它拥有为配置字符串分配的内存

static config_t cfg;
config_t *cf;

// config_destroy(cf); <-- don't!
static config\t cfg;
配置*cf;
//配置销毁(cf) 剪掉中间人

config_lookup_string(cf, "hostname",  hostname);
这要求您永远不要销毁
cfg
,因为它拥有为配置字符串分配的内存

static config_t cfg;
config_t *cf;

// config_destroy(cf); <-- don't!
static config\t cfg;
配置*cf;
//配置销毁(cf) 剪掉中间人

config_lookup_string(cf, "hostname",  hostname);
这要求您永远不要销毁
cfg
,因为它拥有为配置字符串分配的内存

static config_t cfg;
config_t *cf;

// config_destroy(cf); <-- don't!
static config\t cfg;
配置*cf;
//配置销毁(cf) 剪掉中间人

config_lookup_string(cf, "hostname",  hostname);
这要求您永远不要销毁
cfg
,因为它拥有为配置字符串分配的内存

static config_t cfg;
config_t *cf;

// config_destroy(cf); <-- don't!
static config\t cfg;
配置*cf;

//配置销毁(cf);如何调用
parseConfig
?您是否使用所有警告和调试信息(
gcc-Wall-Wextra-g
)进行编译?您是否使用了调试器(
gdb
)?最好为数组提供一个maxsize作为主机名,而不是**主机名,这样函数的用户在提供太小的缓冲区时就不会感到意外。e、 g.字符主机名[100]<代码>解析配置(char*hostName,int-maxLenHostName,…)
如何调用
parseConfig
?您是否使用所有警告和调试信息(
gcc-Wall-Wextra-g
)进行编译?您是否使用了调试器(
gdb
)?最好为数组提供一个maxsize作为主机名,而不是**主机名,这样函数的用户在提供太小的缓冲区时就不会感到意外。e、 g.字符主机名[100]<代码>解析配置(char*hostName,int-maxLenHostName,…)
如何调用
parseConfig
?您是否使用所有警告和调试信息(
gcc-Wall-Wextra-g
)进行编译?您是否使用了调试器(
gdb
)?最好为数组提供一个maxsize作为主机名,而不是**主机名,这样函数的用户在提供太小的缓冲区时就不会感到意外。e、 g.字符主机名[100]<代码>解析配置(char*hostName,int-maxLenHostName,…)
如何调用
parseConfig
?您是否使用所有警告和调试信息(
gcc-Wall-Wextra-g
)进行编译?您是否使用了调试器(
gdb
)?最好为数组提供一个maxsize作为主机名,而不是**主机名,这样函数的用户在提供太小的缓冲区时就不会感到意外。e、 g.字符主机名[100]<代码>解析配置(char*hostName,int-maxLenHostName,…)