char * function decode time()
{
tm *ptm; //time structure
static char timeString[STRLEN]; //hold string from asctime()
ptm = gmtime( (const time_t *)<ime ); //fill in time structure with ltime
if(ptm)
{
strncpy(timeString, asctime( ptm ), sizeof(timeString) );
//EDIT
sprintf(test, "Sting is: %s", timeString);
return timeString;
.
.
} //end function
When I step through the code in the debugger I I can see the value of timeString is:
timeString CXX0017: Error: symbol “timeString” not found
However, when I remove the work “static” from timeString it does fill in correctly with the string but is now a local copy and will be destroyed.
Why am I not able to copy the string from this function into a static char array?
Visual Studio 6.0 – MFC
Thanks.
EDIT
the “test” string does contain the value of timeString.
I guess this is just a debugger issue? but why can’t I see the value of a static array in the debugger watch?
First, function name should be
function_decode_time()notfunction decode time()with local static timeString will initialized entire with ‘\0’, without static its not guaranteed
without static you the return value in calling context is undefined.
strncpy will not added a ‘\0’ in timeString for use “sizeof(timeString)” , see definition;
therefore YOU must added ‘\0’, eg:
If you use local static, your code is not reentrant/thread-safe.