Hi, this is my first time in the forum and I've had some problem with this assignment question.

The code below is compiled using Visual C++ and the error I had was an application error, the instruction at "0x00401..." referenced memory at "0XC...". The memory could not be written.

Can someone tell me where I went wrong or suggest a better way to do this?


/*function InsertString
Takes in :
string A & string B & int C

Operation:
insert B into A at Cth Position

e.g. InsertString("aaaaa","bb",2) will return string "aabbbaaa"*/

#include<stdio.h>
#include<string.h>

char *InsertString(char *A, char *B, int C);

void main()
{
char *A;
char *B;

int x;

printf("\nKey in string1: ");
scanf("%s",A);

printf("\nKey in string2: ");
scanf("%s",B);

printf("\nKey in Position: ");
scanf("%d",&x);

printf("Result of insertion : \n");
printf("%s",InsertString(A,B,x));

}

char *InsertString(char *A, char *B, int C)
{
char *temp;
char *ptr;

if((C>strlen(A))||(C<0))
return NULL;
else
{
ptr=A+C;
strcpy(temp,B);
strcat(temp,ptr);
ptr='\0';
strcat(A,temp);
}

return strcat(A,temp);
}

Thanks:
Jacquiline