I am practicing c++ and this code is supposed to copy an array to another array.output types are character and which is passed by pointer through the function 'f'. my output type is pointer which is pointing to a char array.here is my code:
using namespace std;
void f(char*, char*);
int main()
{
char *a1;
char a2[] = "1234";
f(a1, a2);
cout << a1<< endl;
return 0;
}
void f(char *a, char *b)
{
int len;
len = sizeof(b) / sizeof(b[0]);
a = new char[len];
for (; (*a=*b )!= '\0'; a++, b++)
cout<<*a<<endl;
for (int i=0;i<4;i++)
cout<<a[i]<<endl;
}
- in the function each time for loop runs 'a' elements changes to the unreadable value. at first, it shows right value and then it changes. for example, in the first loop inside the function, it prints right value of 'a' but in second loop it changes it for no reason!
- this function doesn't return any value. as I expect by calling by pointer it has to change the variable but nothing happens. deep explanation is appreciated. I am completely new in c++ programming.