I'm trying to write a function in C that gets an int as a parameter and returns a char array (or a string).
const char * month(int x)
{
char result[40];
if(x<=31) strcpy(result,"can be a day of the month");
else strcpy(result,"cannot be a day of the month");
return result;
}
But my function returns an int, not a string. I have read posts where people might have run into similar situations, but I can't understand how the pointer-type functions work and how to make them return what I want (I have documented a bit about pointers and I have an idea about how they work alone, but I have never tried to write a piece of code that adds some functionality to them, like making a solution more effective or something else.)
resultin an object with automatic scope that gets destroyed when the function returns. The returned pointer is invalid, and dereferencing it is undefined behavior.resultgoes out of scope and life when the function exits. There are many SO question like this.