首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >不推荐使用的转换错误

不推荐使用的转换错误
EN

Stack Overflow用户
提问于 2014-02-05 06:53:15
回答 1查看 158关注 0票数 0

我有以下代码片段:

代码语言:javascript
代码运行次数:0
运行
复制
#include<stdio.h>    //scanf , printf
#include<string.h>  //strtok
#include<stdlib.h>  //realloc
#include<sys/socket.h>  //socket
#include<netinet/in.h> //sockaddr_in
#include<arpa/inet.h>   //getsockname
#include<netdb.h>   //hostent
#include<unistd.h>  //close

int get_whatthe_data(char * , char **);
int hostname_to_ip(char * , char *);
int whatthe_query(char * , char * , char **);
char *str_replace(char *search , char *replace , char *subject );

int main(int argc , char *argv[])
{
    char domain[100] , *data = NULL;

    printf("Enter domain name : ");
    scanf("%s" , domain);

    get_whatthe_data(domain , &data);

    return 0;
}

int get_whatthe_data(char *domain , char **data)
{
    char ext[1024] , *pch , *response = NULL , *response_2 = NULL , *wch , *dt;

    domain = str_replace("http://" , "" , domain);
    domain = str_replace("www." , "" , domain);

    dt = strdup(domain);
    if(dt == NULL)
        {
            printf("strdup failed");
        }
    pch = (char*)strtok(dt , ".");
    while(pch != NULL)
        {
            strcpy(ext , pch);
            pch = strtok(NULL , ".");
        }

并得到以下错误:

代码语言:javascript
代码运行次数:0
运行
复制
main.cpp: In function 'int get_whatthe_data(char*, char**)':
main.cpp:37:46: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
  domain = str_replace("http://" , "" , domain);

诸若此类。

有人能帮我解决这个问题吗?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-02-05 06:56:22

该警告告诉您,您正在向char*分配字符串文字,例如"http://"。由于您不能修改字符串文字,因此应仅将其绑定到指向const char的指针。因此,将您的str_replace签名更改为take const char*

这是问题的简化版本:

代码语言:javascript
代码运行次数:0
运行
复制
char* word = "hello";       // BAD
const char* word = "hello"; // GOOD
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21565261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档