1

I am working on a project where I can to convert a api digit to a char. I have used an array of string pointers to get the conversion. However, I want to return just a single ch, as my api that I am using will only accept a char. So ap_five will return "5". But I want to five to be a single char '5'.

I thought maybe I could cast to a char in the return. However, I just get rubbish.

I am sure there is other ways to solve this, how I am just wondering how can I solve with my present program.

char *digits_conversion[][2]=
{
    {"ap_zero", "0"},
    {"ap_one", "1"},
    {"ap_two", "2"},
    {"ap_three", "3"},
    {"ap_four", "4"},
    {"ap_five", "5"},
    {"ap_six", "6"},
    {"ap_seven", "7"},
    {"ap_eight", "8"},
    {"ap_nine", "9"},
    {"ap_star", "*"},
    {"ap_hash", "#"},
    {NULL, NULL}
};

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; *digits_conversion[i][1]; i++)
    {
        if(strcmp(digits_conversion[i][0], digit) == 0)
        {
            return (char) digits_conversion[i][1];
        }
    }

    return '\0';
}

int main(void)
{
    char ch;

    ch = convert_to_char("ap_five");

    printf("Converted digit: %c\n", ch);

    return 0;
}

4 Answers 4

8

The simplest fix to your existing code is just to change:

return (char) digits_conversion[i][1];

into

return digits_conversion[i][1][0];

However, you might find that changing digits_conversion into an array of structures will give you code that is easier to understand and maintain. For example:

struct digit_mapping {
    char *api_name;
    char digit;
};
struct digit_mapping conversion_table[] = {
    { "ap_zero", '0' },
    {"ap_one", '1'},
    {"ap_two", '2'},
    {"ap_three", '3'},
    {"ap_four", '4'},
    {"ap_five", '5'},
    {"ap_six", '6'},
    {"ap_seven", '7'},
    {"ap_eight", '8'},
    {"ap_nine", '9'},
    {"ap_star", '*'},
    {"ap_hash", '#'},
    {NULL, '\0'}
};

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; conversion_table[i].digit; i++)
    {
            if(strcmp(conversion_table[i].api_name, digit) == 0)
            {
                    return conversion_table[i].digit;
            }
    }

    return '\0';
}

int main(void)
{
    char ch;

    ch = convert_to_char("ap_five");

    printf("Converted digit: %c\n", ch);

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am new to c programming as I am coming from .Net. However, digits_conversion[i][1][0]; that worked. But I can't see how it works. Why does indexing a third element give the char? Where did the third element come from when I have a 2 dimensional array? Thanks,
@robUK: strings in C are character arrays, so using 2 subscripts gets you down to (for example) "8" or "#", and then the third subscript [0] gives you the first character in the string "#" which is '#'.
@robUK: A String in C is, practically speaking, just an array of characters. So, grabbing the item at index 0 of the String will return a character, the first character in the String. That bottommost array layer, the array of characters comprising a String, is abstracted away when you use a String.
3
digits_conversion[i][1]

is a C-style string. Use:

digits_conversion[ i ][ 1 ][ 0 ] // if you want the character 

char convert_to_char(const char *digit)
{
    int i = 0;
    for(i = 0; *digits_conversion[i][1]; i++)
    {
            if(strcmp(digits_conversion[i][0], digit) == 0)
            {
                    return digits_conversion[i][1][0];
            }
    }

    return '\0';
}

Comments

2
return digits_conversion[i][1][0];

Comments

0

The termination of your 'for' loop looks incorrect:

//...
    {"NULL", "NULL"}
//...
//...
for(i = 0; *digits_conversion[i][1]; i++)
//...

The "NULL" you've got at the end of your array is literally, the string "NULL". If you pass a string that doesn't match any of the "ap_zero" through "ap_hash" your program will likely crash as the loop starts dereferencing garbage beyond the end of your array.

Eric fixed this in his example, but I thought I'd point this out specifically.

1 Comment

Thanks for pointing that out. I guess I was too quick then I was typing it in here. I have now edited my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.