"分配丢弃“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?
答
输入
是一个指向一个恒定的字符,而您将其分配给一个指针非恒字符。 这这里可能是你一个有趣的阅读。
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.