0

I am still relatively new to C programming and have come across an error I haven't seen before. I have wrote a program that takes in two ints and converts the first into its respective form of radix based on the second input. I am not asking how to solve the problem I'm just asking where I went wrong to receive this error. I have done some research and know that segmentation faults have to do with pointers and I have played around with mine and have had no luck on getting rid of this error. Any help would be much appreciated!

    #include<stdio.h>

    void decimalToRadix(int d, int r, char *toRadix);   

    int main(void){

        int decimal, radix;
        char toRadixForm[100];

        printf("Enter a decimal number: ");
        scanf("%d",&decimal);

        printf("Enter radix number: ");
        scanf("%d",radix);

        decimalToRadix(decimal, radix, toRadixForm);
        puts("");
    return 0;
    }

    void decimalToRadix(int decimal, int radix, char *toRadix){

        int result;
        int i=1,x,temp;
        result=decimal;

        //will loop until result is equal to 0
        while(result!=0){

        //get the remainder
        temp=result%radix;

        //if<10 add 48 so character format stored values are from 0-9
        if(temp<10)
        temp=temp+48;

        //if greater that or equal to 10 add 55 to it stores values A-Z
        else
        temp=temp+55;

        toRadix[i++]=temp;
        result=result/radix;

     }
    printf("The value of the number you entered, %d, to radix form is ", decimal);

    for(x=i-1; x>0; x--){
       printf("%c", toRadix[x]);

}
Corey
  • 23
  • 1
  • 5
  • 2
    The way to debug this (and most other bugs) is to use a debugger. – kaylum May 01 '17 at 02:35
  • @kaylum I usually just step through it line by line when I get an error. Not too familiar with any debuggers so maybe I should re-word my original post. I appreciate your response. – Corey May 01 '17 at 02:39
  • 2
    Compare `scanf("%d",radix);` and `scanf("%d",&decimal);` carefully – BLUEPIXY May 01 '17 at 02:41
  • 1
    @BLUEPIXY ah, I see where I may have gone wrong. Looks like I forgot an ampersand(&) after radix. – Corey May 01 '17 at 02:46

1 Answers1

-1

The reason you might be getting this is because of a typo i guess. You're missing a & in the scanf argument list on line 14. You should instead do : scanf("%d",&radix);. You were getting the Segmentation fault because scanf expects the memory address of the variable it is supposed to read; because it's the only way you can change a variable outside it's scope. But your're passing scanf("%d", radix) and in this case radix can contain 0 or any garbage value. When your program tries to access that memory address, which is not supposed to be read by the program, OS terminates the program giving Segmentation Fault. On changing this i was getting the output:

~/Documents/src : $ ./a.out 
Enter a decimal number: 12
Enter radix number: 2
The value of the number you entered, 12, to radix form is 1100
Community
  • 1
  • 1
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
  • Thanks! @BLUEPIXY pointed that out above. Not sure how I forgot that and overlooked it but it obviously is necessary there for this program to run. I appreciate your response. – Corey May 01 '17 at 02:50