Original Post
hi
Could somebody tell me why I cant return the correct modified array contents.
void changeArray( char *arr )
{
// resize array
char* tmp = new char[10];
memset(tmp,0,10);
memcpy(tmp, arr, 4); // copy old contents 'STAR'
delete [] arr;
arr = tmp;
}
main()
{
char *a = new char[4];
memset(a,0,4);
a[0] = 'S'; a[1] = 'T'; a[2] = 'A'; a[3] = 'R';
change( a );
// should be 'S' using a[0] but it prints out some junk
}
When printing out from a[] I cant see the old contents that I copied into tmp and tehn put itback into a[].
Why? Should I pass the array like this
change( &a);
but then this is like double pointer am I right. How should I delete it if its correct so there are no mem leaks.
When I try this it causes errors.
void changeArray( char **arr )
{
// resize array
char* tmp = new char[10];
memset(tmp,0,10);
memcpy(tmp, *arr, 4); // copy old contents 'STAR'
for( int i=0; i<4; ++i)
delete [i] arr;
*arr = tmp;
}
Sorry I just started programming.
[edited by - papa on May 5, 2004 12:49:13 PM]