I wrote the following 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++;
}
}
and I’m getting the above warning on pLastLetter[i] = &str[strIndex-1];
pLastLetter is a pointers array that points to a char in str[].
Anyone knows why I’m getting it and how to fix it?
Well, as you said yourself,
pLastLetteris an array ofchar *pointers, whilestris an array ofconst char. The&str[strIndex-1]expression has typeconst char*. You are not allowed to assign aconst char*value to achar *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.As for “how to fix it”… It depends on what you are trying to do. Either make
pLastLetteran array ofconst char*or remove theconstfromstr.