Home>

How do you write a function that returns an integer as a string?

As an example

include

void ans (char b);

int main (void)
{
char a = 1;
ans (a);
}

char ans (char b)
{
char * name_b;

switch (b)
{
case 1:
name_b = ("OK");
default:
break;
}
return * name_b;
}

I'm sorry halfway, but what I want to do is
Put the value of a in the function ans. In the function, depending on the value of a in the switch statement
I want to return a character. a can only be referred to as a switch statement within a function.
I created a new string variable in the function
I am trying to put a fixed character string in the switch statement in it, but I could not.
How can I do it? Answer please. .

c
  • Answer # 1

      

    Returns an integer as a string

    I don't understand the meaning of the story ...

    // If you insert a value into some argument,
    // A feeling of returning a (?) Character string (a pointer to the beginning of) corresponding to that value?
    const char * ans (char b)
    {
        switch (b)
        {
        case 1: return "OK";break;
        case 2: return "Kitten";break;
        default: return "Other";break;
        }
    }
    // A type that plunges a character string into the area specified by the argument.
    // Put the character string corresponding to the value of argument b in the area pointed to by pBuff.
    errno_t ans_TypeB (
        char b,
        char * pBuff,
        size_t BuffSize // size of the area pointed to by pBuff
    )
    {return strcpy_s (pBuff, BuffSize, ans (b));}

  • Answer # 2

    It is difficult to write a "function that returns a character string" in C language.

    This is because the character string is handled as an array of characters, and the handling of the array itself is complicated in C language.

    Get a pointer from the function calling side and fill it there

    staticSecures a large area and returns a pointer to it

    mallocAnd return a pointer to that area

    Each has advantages and disadvantages, but the most general is the first pattern.