So I have a homework problem that asks that I write a function that takes an array of chars, a char, and an int pointer. The function should loop through the array of chars, ignore any instance of the second parameter char, and create a new array which is composed of the characters in the original array other than instances of the second parameter char. Then the original length parameter needs to be changed to the new length, and the new array should be returned. I'm very new to C++ so pointers are a new concept which I haven't fully grasped yet.
char *Problem5(char chars[], char letter, int *length){
int newLength=0;
int counter=0;
for(int i=0;i<*length;i++){
if(chars[i]!=letter){
newLength++;
}
}
char newChars[newLength];
for(int x=0;x<*length;x++){
if(chars[x]!=letter){
newChars[counter]=chars[x];
counter++;
}
}
*length=newLength;
return newChars;
}
There is my function, I know the algorithm works fine but it won't compile.
char chars2[10] = {'g','g','c','g','a','g','g','g','t','g'};
printArray(chars2,10);
char *arrayPointer;
arrayPointer = Problem5(chars2,'g',length1);
printArray(arrayPointer,3);
And this is the section of my main function where it is called. I need to print out the resultant array, and so I try to assign it to a pointer, but this line:
arrayPointer = Problem5(chars2,'g',length1);
Throws an error: Invalid conversion from 'char' to 'char*' A little insight would be very much appreciated because I have no clue what I'm doing wrong.