I just did a recursive function and again it seems to suffer the 'Instruction at ".." referenced memory at "ox..." Memory cannot be written.
The problem isn't in your recursive function, it's in your call to scanf. Remember that a local pointer starts out with a junk memory address, if you try to write to that address then you'll probably get a run-time error. There're three things you want to do when you use scanf: 1. Make sure the memory can be written to. 2. Make sure you don't overwrite your buffers. 3. Check the return value of scanf to make sure everything is kosher. The following code does all of this, if scanf doesn't return success then it doesn't call the function
Code:
int main(void)
{
char str1[1001];
int B=0;
printf("Key in string to process: ");
/*
* 1. str1 is an array, you own the memory
* 2. The field width protects buffer overwrite
* 3. A successful return is 1, we check for it.
*/
if (scanf("%1000s",str1) == 1)
PrintChar(str1,B);
return 0;
}
And lastly, unless you're on a freestanding implementation the main function must return an int. If you don't know whether your on a freestanding implementation, you aren't.