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

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Stevie
  • 149
  • 6
  • 17
  • 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. – Sam Varshavchik Oct 16 '16 at 20:49
  • `result` goes out of scope and life when the function exits. There are many SO question like this. – Weather Vane Oct 16 '16 at 20:49
  • Is this C code or C++ code? The answers for the two languages are completely different. – David Schwartz Oct 16 '16 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. –  Oct 16 '16 at 20:53
  • this code is written in C – Stevie Oct 16 '16 at 20:53
  • Possible duplicate of -- oops, never mind. So many to choose from (instead of answering yet again!) and I stillmanaged to pick a wrong one. – Jongware Oct 16 '16 at 20:54

2 Answers2

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";
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
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.

Kiloreux
  • 2,220
  • 1
  • 17
  • 24
  • 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. – Stevie Oct 16 '16 at 21:06
  • Assuming you are using C and not C++, there is no string support in C so you need char[] declared. – Kiloreux Oct 16 '16 at 21:07
  • It works totally fine. Thanks :) – Stevie Oct 16 '16 at 21:10