reverse a string using character arrays and recursion, some Thread 1: EXC_BAD_ACCESS error

I’m trying to reverse a string using character arrays and recursion but I get “Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)” the compiler asks for about 7 inputs and then starts asking for commands, whereas it should ask for input once and then print the answer on the next line. The program runs fine when I use loops, it starts doing weird things when I use recursion.

char* reverse(char ss[30], int l){
    char *p = new char;
    int c=0;
        reverse(ss, l);
    return p;

int main(int argc, const char * argv[]) {
    string s;
    char ss[30];
    for(int i=0; i<s.length(); i++){
    char *answer;
    answer=reverse(ss, s.length());
    return 0;


So first we read the input from the user and store it in a string called s; we get the size add put it in a char array (after all a char array can be thought of as a string), the extra one is just for the ‘’ character. next I pass it into the reverse function and then print out the array.

#include <iostream>
#include <string>
using namespace std;

int main()
    string s;
    unsigned int StringSz = s.length();
    char arr[StringSz+1];
    arr[StringSz] = '';
    cout << arr;
    return 0;

Now how does our function works?

We pass the array, the string and the position we are currently in, then we switch the first element with the last element, we switch the second element with the one before the last and do this until we reach the end of the string.

void reverse(char* arr,string& s,int start=0)
    arr[start] = s[s.length()-start-1];