Why do I get an access violation when I try to change the value of individual characters in a char array via pointers?
If I pass a pointer-to-pointer-to-char (char **string) and try to change any of it's elements I get an access violation. This has happened with a few projects working with strings and I was never able to figure out why so I just ignored it, but now it's bugging me
So I'm attempting a recursive function to reverse a string in place and this is what I've got: I'll bold the line where I am getting an access violation.
Code:
#include <stdio.h>
#include "recursive.h"
char *buf;
int buf_p;
void reverse_string(void);
char *reverse(char **string){
buf = *string;
buf_p = 0;
reverse_string();
buf[buf_p] = '\0';
return *string;
}
void reverse_string(){
char c;
if((c = buf[buf_p++]) != '\0')
reverse_string();
else{
buf_p = 0;
return;
}
buf[buf_p++] = c;
}