# 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, int l){
char *p = new char;
int c=0;
if(c!=l+1){
p[c]=ss[l-c-1];
c++;
reverse(ss, l);
}
return p;
}

int main(int argc, const char * argv[]) {
string s;
cin>>s;
char ss;
for(int i=0; i<s.length(); i++){
ss[i]=s[i];
}
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;
cin>>s;
unsigned int StringSz = s.length();
char arr[StringSz+1];
arr[StringSz] = '';
reverse(arr,s);
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)
{
if(start==s.length())
return;
arr[start] = s[s.length()-start-1];
reverse(arr,s,start+1);

}
```