Please help me understand what I am doing wrong with my double pointer arithmetic. I know I clearly doing something incorrect but what? Look at the line where I marked, “bad pointer”. My intuition told me that this should work, but I guess not. Programmed in C.
/*
*This function searches for prefixes within the string array.
*@param stringArray array containing strings
*@param searchPrefix a string or characters to search for at beginning of string
*@return void
*/
void prefixSearch(char* stringArray[SIZE], char* searchPrefix){
int count = strlength(searchPrefix);//size of the prefix search
while(count > 0){
if(**stringArray == *searchPrefix){
printf("%c match %c\n", **stringArray, *searchPrefix);
**stringArray++;//want to move to next character, instead get bad pointer.
*searchPrefix++;//moves to next char
}else{
stringArray++;//no match, go to next string
}
count--;//decrement
}
}
The
*and++operators have the same precedence, and right-to-left associativity, so the line that’s not working parses like this:I don’t actually understand your code, but that can’t be what you intended since the dereference operators have no effect. Presumably you wanted this instead: