在下面的功能:「分配丟棄‘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;
}
我得到assignment discards 'const' qualifier from pointer target type
爲線rest = input + i + 2
,但是,正如你所看到的,其餘的是不是一個常數指針。我在這裏做錯了什麼?
但是'input'是'const' ... – Mysticial 2012-04-11 20:26:05
「正如你所看到的,休息不是一個常量指針」。你不能給一個非const('rest')成員分配一個const('input'),這正是錯誤信息所說的。你試圖把它扔掉(拋棄),但你不能那樣做。 – 2012-04-11 20:26:14