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;
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[30];
for(int i=0; i<s.length(); i++){
ss[i]=s[i];
}
char *answer;
answer=reverse(ss, s.length());
cout<<answer<<endl;
return 0;
}