Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So my CS professor gave the whole class a simple assignment. "Write a recursive function that will swap the order of a section in an array of chars." I thought to myself, "Easy. I'll finish this up in about 5 minutes and I'll get to work on my Trig homework before I leave."

This is not what happened. An hour later, the professor and I are both wondering what on Earth is going wrong.

void swap(char charList, int start, int stop);

int main()
    char myList[] = {'a', 'b', 'c', 'D', 'E', 'F', 'g', 'h', 'i', 'j'};
    int size = 9;
    printCharList(myList, size);
    swap(myList, 3, 5);           //<--- It doesn't seem to like this call
    return 0;

void swap(char charList[], int start, int stop)
    char temp
    if(start < stop)
        temp = charList[stop];
        charList[start] = charList[stop];
        charList[stop] = temp;
        swap(charList, start+1, stop-1);
        std::cout << "start > stop\n";

The code is stupidly simple, so you can imagine my confusion when it refused to compile. It keeps throwing up an error message suggesting that I am trying to convert a pointer to a char, but I'm almost positive that I've done no such thing. I'm sure the problem is right in front of my face, but I have had no luck in finding it. Could you help a guy out?

share|improve this question

closed as off topic by Blrfl, MainMa, Frank Shearar, Martijn Pieters, thorsten müller Mar 27 '13 at 8:10

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

you're missing a semicolon, is that in the actual code as well? – jozefg Mar 27 '13 at 2:12
What sort of compiler are you using that doesn't warn about mismatched prototype declaration and function definition? All modern compilers do that. – Greg Hewgill Mar 27 '13 at 3:45
There's another bug in the program: temp remembers what charList[stop] was, and then you overwrite charList[start] without saving it. – Michael Shaw Mar 27 '13 at 3:45
Yes, I noticed the temp error right after i posted this, but I was kind of distracted by other simple mistakes. For the record, I tried this program on two different compilers. Both gave different (but similar) messages. MS Visual C++ Visual 2010 Express/ /error C2664: 'swap' : cannot convert parameter 1 from 'char [10]' to 'char'/ /Code::Blocks 10.05/ /main.cpp|35|error: invalid conversion from 'char*' to 'char' / /main.cpp|35|error: initializing argument 1 of 'void swap(char, int, int)'| – anthony Mar 27 '13 at 5:08
up vote 4 down vote accepted

In the header, you declare the signature as

void swap(char charList, int start, int stop);

you probably want to declare it as

void swap(char charList[], int start, int stop);
share|improve this answer
Mikes answer is technically correct, but it is absolutely essential you understand why the code did not compile and what the correction he has suggested does. Clear understanding C++ (and C) arrays and pointers is an enormous part of using these languages – mattnz Mar 27 '13 at 2:59
You've got to be kidding me. THAT was the problem? Wow. Yeah, that was exactly what I was looking for. I think I understand WHY this happened. swap() was expecting a char, but it got a pointer to an array of chars instead. That's not even remotely the same thing. I think I've got that right. – anthony Mar 27 '13 at 4:55
Indeed. But it looks purely like a typo. Not a misunderstanding of the difference between an array and an object. – Michael Brown Mar 27 '13 at 5:06
Well, thanks for the help Mike. And everyone else. I looked at the declaration several times, but it just never clicked. – anthony Mar 27 '13 at 5:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.