-1
char * findCombination(char * c)
{
    switch(c)
    {
    case "2":
        char c[] = {'a', 'b', 'c','\0'};
        return c;
        break;
    case "3":
        char c[] = {'d', 'e', 'f', '\0'};
        return c;
        break;
    case "4":
        char c[] = {'g', 'h', 'i', '\0'};
        return c;
        break;
    case "5":
        char c[] = {'j', 'k', 'l', '\0'};
        return c;
    case "6":
        char c[] = {'m', 'n', 'o', '\0'};
        return c;
        break;
    case "7":
        char c[] = {'p', 'q', 'r', 's', '\0'};
        return c;
        break;
    case "8":
        char c[] = {'t', 'u', 'v', '\0'};
        return c;
        break;
    case "9":
        char c[] = {'w', 'x', 'y', 'z', '\0'};
        return c;
        break;
    default:
        break;

    }
}
int main()
{
    char number[2];
    scanf("%s",&number);
    char array[] = findCombination(number);
    return 0;
}

Here I want the function to return a char array whenever I pass a string. How can I return char array from the function? As an example if input char is 2 then the function will return char array {'a', 'b', 'c','\0'}

  • 3
    Possible duplicate of [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – GSerg Apr 01 '19 at 17:13
  • `char *array = findCombination(number);` and you better allocate memory for that array or you're gonna have some bed time. – Hagai Wild Apr 01 '19 at 17:15

2 Answers2

0

The problem here is that you're creating a local array and then returning a pointer to it. The array then goes out of scope. When you then try to access that out of scope array, you invoke undefined behavior.

This is also invalid:

char array[] = findCombination(number);

Because an array must be initialized with a constant expression.

You also can't pass a char * as the condition of a switch, since it only looks at the numeric value.

Assuming you don't want to modify the array you pass back, and given that the arrays you return are actually strings, you can return string constants from the function. String constants have a lifetime of the full runtime of the program, so they're safe to return. You would then assign the return value to a pointer.

You should also change the function to accept a single char and check that.

const char * findCombination(char c)
{
    switch(c)
    {
    case '2':
        return "abc";
    case '3':
        return "def";
    case '4':
        return "ghi";
    case '5':
        return "jkl";
    case '6':
        return "mno";
    case '7':
        return "pqrs";
    case '8':
        return "tuv";
    case '9':
        return "wxyz";
    default:   // you need to return something in the default case as well.
        return "";
    }
}

int main()
{
    char number;
    scanf(" %c",&number);
    const char *array = findCombination(number);
    printf("returned %s\n", array);
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
0

First of all, you can't use switch on a string - the argument must be integral. You can switch on individual characters like so:

switch( c[0] ) // or switch ( *c )
{
  case '2': // note single quote instead of double quote
    ...
  case '3': 
    ... 

You cannot return an array from a function, nor can you initialize an array with a function call. Honestly, your best option is to pass the target array as an argument:

void findCombination( int number, char *combo, size_t combo_size )
{
  switch( number )
  {
    case 2:
      strcpy( combo, "abc" );
      break;

    case 3:
      strcpy( combo, "def" );
      break;
    ...
  }
}

int main( void )
{
  int number;
  char combo[5];

  scanf( "%d", &number );
  getCombo( number, combo, sizeof combo );
  ...
}
John Bode
  • 119,563
  • 19
  • 122
  • 198