"分配丢弃“const”限定符"错误的非const指针 [英] "assignment discards 'const' qualifier" error on non-const pointer

查看:155
本文介绍了"分配丢弃“const”限定符"错误的非const指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数:

char *mystrtok(const char *input, const char *delim,char *rest) {
    int i;
    for (i = 0; input[i] != *delim && input[i] != '\0'; ++i) {
        continue;
    }
    char *result = malloc(sizeof(char) * (i + 2));
    memcpy(result, input, i + 1);
    result[i + 1] = '\0';
    if (input[i + 1] != '\0') 
        rest = input + i + 2;
    else
        rest = NULL;
    return result;
}

我收到分配丢弃'常量'指针目标类型的限定词的行休息=输入+ I + 2 ,但是,正如你所看到的,其余的是不是一个常量指针。我在做什么错在这里?

I am getting assignment discards 'const' qualifier from pointer target type for the line rest = input + i + 2, however, as you can see, rest is not a constant pointer. What am I doing wrong here?

推荐答案

输入是一个指向一个恒定的字符,而您将其分配给一个指针非恒字符。 <一href=\"http://www.$c$cguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm\"相对=nofollow>这这里可能是你一个有趣的阅读。

input is a pointer to a constant char, and you're assigning it to a pointer to a non-constant char. This here might be an interesting reading for you.

这篇关于&QUOT;分配丢弃“const”限定符&QUOT;错误的非const指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆