1

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.

Drew
  • 11
  • 1
  • Your example code seems to compile (http://coliru.stacked-crooked.com/a/350344306fe84a68) so I'm not sure why you're getting that error. Note that when you return newChars from the Problem5() function, it will be deleted and the pointer will be pointing to garbage. You need to allocate newChars using 'new' or 'malloc', etc – Prismatic Feb 26 '15 at 04:16
  • Your algorithm looks ok and your code looks pretty much all right. I am a little skeptical of this line : – Wilf Rosenbaum Feb 26 '15 at 05:26

1 Answers1

0

Your algorithm looks ok and your code looks pretty much all right. I am a little skeptical of this line :

char newChars[newLength];

because you can't allocate an array at compile time with a variable size. Is it possible that the code you posted isn't exactly the code you tried to compile?

Change the line above to

char *newChars = new char[newLength]; 

and the array will be allocated dynamically at run time. I cannot understand why you would get an "Invalid conversion from 'char' to 'char*'" error compiling

arrayPointer = Problem5(chars2,'g',length1); 

given the definitions that you've provided. The only things that have type "char*" are the first parameter to and return value from Problem5, and those look to be the correct types from what you've posted.

Wilf Rosenbaum
  • 518
  • 3
  • 10
  • Thanks for answering! My code was compiling without the change you just gave me believe it or not, I'm not sure why. Maybe I have an issue with my compiler because it's still throwing that same error. – Drew Feb 27 '15 at 00:32
  • Scratch that, just figured it out. In the separate file that held my main function, I forgot to put an asterisk in front of the function declaration... *facepalm* – Drew Feb 27 '15 at 00:42