Re: problems with char * return from a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Vardhan, Sundara (GE Infra, Energy) wrote:
Hi All

I am calling a function x within strcpy as follows

strcpy(a,x("sample text","default text"));

x is defined as follows

char * x(char *m, char *n)
{
	char *return_val=NULL;
	if (check m is in database)
		return_val=m;
	else
		return_val=n;
	return(return_val);
}
 This causes  the array a to have a corrupted string. The string is either m or n but with illegal characters appended.

The function x returns a pointer local to it on the stack which is why it is corrupted when its caller gets around to using it. Instead of returning <return_val>, try return m (or n) assuming those values aren't also pointers to values local to the function which produces them.
--
_______________________________________________________________
Bob Smith - bsmith@xxxxxxxxxxxxxxx - http://www.sudleyplace.com


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux