Thursday, July 10, 2008

Return values in function argument Pointers

Pointers Sample application:
----------------------------------------
int Function( char* arr)
{
strcpy(arr,"sun");
return 0;
}

void Main()
{
char arr[3];
Function(arr);
printf("\n %s",arr);
}
Expected Output : sun
Actual Result : I got some invalid junk data;


So I modified the main()fn call as follows:

void Main()
{
char arr[3];
Function(&arr[0]);
printf("\n %s",arr);
}
the Output is : sun

No comments: