-2

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.)

6
  • 1
    result in an object with automatic scope that gets destroyed when the function returns. The returned pointer is invalid, and dereferencing it is undefined behavior. Commented Oct 16, 2016 at 20:49
  • result goes out of scope and life when the function exits. There are many SO question like this. Commented Oct 16, 2016 at 20:49
  • Is this C code or C++ code? The answers for the two languages are completely different. Commented Oct 16, 2016 at 20:52
  • Make up your mind with the tags -- some the approaches that are normal for C are things you usually really shouldn't do in C++. Conversely, some of the things you would do in C++ you can't do in C. Commented Oct 16, 2016 at 20:53
  • this code is written in C Commented Oct 16, 2016 at 20:53

2 Answers 2

3
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;
}

This doesn't make sense. You return a pointer to the array, but after the function returns, the array no longer exists since result is local to the function.

For C:

const char * month(int x)
{
    if(x<=31) return "can be a day of the month";
    return "cannot be a day of the month";
}

For C++:

std::string month(int x)
{
    if(x<=31) return "can be a day of the month";
    return "cannot be a day of the month";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Considering this is a C code. (not sure if C++) Your best choice here is to have result declared outside the function scope and then pass a pointer inside the function that you are using that you can fill with your data (be sure to not overflow). In what you are using, result will be destroyed and you won't be able to use it.

void month(int x, char* result)
{
    if(x<=31) strcpy(result,"can be a day of the month");
    else strcpy(result,"cannot be a day of the month")
}

That was only a suggestion you could return instead some error code or whatever you want.

3 Comments

In order to store the value of the result, do I need a char[] type or a string when I call the function in int main()? I know it's a stupid question but I haven't worked with pointers before.
Assuming you are using C and not C++, there is no string support in C so you need char[] declared.
It works totally fine. Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.