what is wrong here??? dynamic array of chars
I am trying to implement the simple strcat() function by using pointers.
It states 'Access violation writing location' error, and I don't understand why...
I also tried to implement it by not using indexes (such as here), but by using
the pointers themselves (e.g, t++, s++, etc.), and of course, the same problem
occurs again... see // comment at the line the problem occurs.
thanks in advance.
Code:
#include <iostream>
using namespace std;
void strcat(char* t, char* s);
void main(){
char* t, *s;
t=new char[200];
s=new char[100];
if (t==NULL || s==NULL)
exit(1);
else
{
t="Hello world";
s="avi";
strcat(t,s);
cout<<t;
}
delete []t;
delete []s;
}
//Ans. 3 (a)
void strcat(char* t, char* s){
int iRead, iWrite;
iRead=0;
iWrite=0;
while (t[iWrite]!='\0')
iWrite++;
while (s[iRead]!='\0')
{
t[iWrite]=s[iRead]; //problem is here
iRead++;
iWrite++;
}
t[iWrite]='\0';
}