警告:赋值丢弃了指针目标类型的限定 [英] warning: assignment discards qualifiers from pointer target type
问题描述
我写了下面code:
void buildArrays(char *pLastLetter[],int length[], int size, const char str[]) {
int i;
int strIndex = 0;
int letterCounter = 0;
for (i=0; i<size; i++) {
while ( (str[strIndex] != SEPERATOR) || (str[strIndex] != '\0') ) {
letterCounter++;
strIndex++;
}
pLastLetter[i] = &str[strIndex-1];
length[i] = letterCounter;
letterCounter = 0;
strIndex++;
}
}
和我得到的 pLastLetter上述警告[I] =安培; STR [strIndex-1];
pLastLetter是一个数组的指针指向一个char str中[]。
任何人都知道为什么我得到它,以及如何解决它?
Anyone knows why I'm getting it and how to fix it?
推荐答案
嗯,你说的你自己, pLastLetter
是字符数组*
指针,而 STR
是为const char
的数组。在&放大器; STR [strIndex-1]
前pression的类型为const char *
。您不得指派为const char *
值到的char *
指针。这将违反常量,正确性的规则。其实,你在做什么是C. C编译器的错误报告的传统作为一个单纯的警告,以避免破坏一些老的传统code。
Well, as you said yourself, pLastLetter
is an array of char *
pointers, while str
is an array of const char
. The &str[strIndex-1]
expression has type const char*
. You are not allowed to assign a const char*
value to a char *
pointer. That would violate the rules of const-correctness. In fact, what you are doing is an error in C. C compilers traditionally report it as a mere "warning" to avoid breaking some old legacy code.
至于如何解决......这取决于你正在尝试做的。要么让 pLastLetter
为const char *的数组
或删除常量
从 STR
。
As for "how to fix it"... It depends on what you are trying to do. Either make pLastLetter
an array of const char*
or remove the const
from str
.
这篇关于警告:赋值丢弃了指针目标类型的限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!