-1

This code is giving segmentation error some one please help me in finding the error.

#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]);
}
Vality
  • 6,577
  • 3
  • 27
  • 48
user218324
  • 3
  • 1
  • 3

1 Answers1

2

Don't dereference single_numbers[thousands] and so on. These are already char*, if you dereference them, you make them char, but printf expects char*, so it defererences a given char, which leads to a segfault.

flyx
  • 35,506
  • 7
  • 89
  • 126