将数字打印成单词。我写过这个程序。但我得到分段错误

时间:2014-12-08 14:10:59

标签: c segmentation-fault

这段代码给出了分段错误,请帮我找错。

#include<stdio.h>

void main()
{
    char *single_numbers[10] =
    {
        "","one","two","three","four","five"
        ,"six","seven","eight","nine"
    };
    char *tens_numbers[8] =
    {
        "twenty","thirty","fourty","fifty"
        ,"sixty","seventy","eighty","ninety"
    };
    char *teens[9] =
    {
        "eleven","twelve","thirteen","fourteen"
        ,"fifteen","sixteen","seventeen"
        ,"ëighteen","nineteen"
    };
    int number,thousands,hundreds,tens,units,temp;
    printf("enter the number");
    scanf("%d",&number);
    thousands=number/1000;
    hundreds=(number%1000)/100;
    temp=(number%100);
    tens=temp/10;
    units=number%10;
    if(temp>=20)
        printf("%sthousand %shundred %s%s"
            ,*single_numbers[thousands]
            ,*single_numbers[hundreds]
            ,*tens_numbers[tens - 2]
            ,*single_numbers[units]);
    else
        printf("%sthousand %shundred %s"
            ,*single_numbers[thousands]
            ,*single_numbers[hundreds]
            ,*teens[temp - 10]);
}

1 个答案:

答案 0 :(得分:2)

请勿取消引用single_numbers[thousands]等。这些已经char*,如果您取消引用它们,则表示charprintf期望char*,因此它会引用给定的char,这会导致一个段错误。