-3

The code is compiling fine. When I execute I just get the message (Segmentation fault(core dumped)). Any help would be greatly appreciated!

The program is supposed to change the base of the arguments entered. for example ./convert 10 2 5 6 7 input base: 10 Output base: 2 arguments in input base: 5 6 7

I'm just beginning to learn c... any help you could provide would be great! Thanks!

Edited : no longer having issues with segmentation fault. Thank you.

#include <stdio.h>
#include <string.h>




int makeInt(char digits[]) {
    int num = 0;
    int i = 0;
    while (digits[i] != '\0') {
    num = num * 10 + (digits[i] - '0');
    i++;
 }
 return num;
}


//converts from original base to base 10
int makeBase10(char digits[],int a) {
    int num = 0;
    int pow = 1;
    int i = 0;
    int x = 0;
    int length = strlen(digits);
    char k;
    for (i = length -1; i > -1; i--){
        k = digits[i];
        if(k <= '9' && k >= '0'){
            x = (int)k - '0';
        }
        else{
            x = (int)k - 'a' + 10;

        }
        num = num + (x*pow);
        pow = pow * a;

    }
    return num;
}
//changes number from base 10 to knew base
// and returns as string
char * toString(char str[], int num, int b) {

    int i = 0;
    char x;
    int mod = 0;
    while(0 < num){
        mod = num % b;
        if(mod < 10 && mod > -1){
            x = (char)(num + '0');
        }
        else{
            x = (char)(num - 10 + 'a');
        }

        str[i] =(int) x;
        num = num/b;
    }
    str[i] = '\0';

    int length = i + 1;
    int j = 0;
    char *reverse;
    for(i = length -2; i >= 0; i--){

        reverse[i] = str[j];
        j++;
    }
    reverse[length-1] = '\0';
    return reverse;
}



 int main(int argc, char *argv[]) {
    int i;
    int num;
    int a;
    int b;
    char str[33]; //binary could be up to 32 + '\0'
    a = makeInt(argv[0]);  //input base
    b = makeInt(argv[1]);;  //output base
    printf("make int worked and input works");
    num = 0;
    i = 2;
    while (i < argc) {
        num = makeBase10(argv[i], a);
        printf("%s\n", toString(str, num, b));
            i++;
    }


}
Graham
  • 1
  • 1
  • How about doing some debug? Add some prints to find out how far your program gets before it crashes. You'll be able to figure out which bit crashes and probably figure out the problem - all without asking a "debug my code" question on SO. – John3136 May 04 '17 at 02:41
  • I'd be having a close look at the reverse pointer too. – nfproductions May 04 '17 at 02:43
  • I can't, I put a printf statement in the first line in main and still nothing... it must be an error in how I've formatted this code... honestly I'm unfamiliar with this language and am just unsure how to proceed. – Graham May 04 '17 at 02:45
  • and Ill check that nfproductions – Graham May 04 '17 at 02:46
  • If you're just learning, it would be worthwhile to try using `gdb(1)` to step through your program and finding the seg fault. Also, `argv[0]` is not what you think it is. Try printing it out. – ktbiz May 04 '17 at 02:46
  • naming a variable `pow` is a bad idea – phuclv May 04 '17 at 03:15

2 Answers2

1

You are using an uninitialized pointer.

int length = i + 1;
int j = 0;
char *reverse; // here is the problem
for(i = length -2; i >= 0; i--){

    reverse[i] = str[j];
    j++;
}
reverse[length-1] = '\0';

When using an uninitialized pointer you are writing to whatever memory address happens to be stored in that variable. No sane value has been given. Hence you are writing to some memory location not meant for apps to write to. Computer memory is segmented. Each area of memory has an owner, who can write there. It's a security precaution and it prevents bugs from making a mess in the system. You must initialize first either with malloc

char *reverse = malloc(length);

or if you know at compile time a maximum size you can do:

char reverse[MAX_SIZE];

P.S. In order to reverse you don't traverse the whole string until the beginning just half. Instead of for(i = length -2; i >= 0; i--)
write: for(i = length -2; i >= length / 2; i--)

MotKohn
  • 3,485
  • 1
  • 24
  • 41
  • Thanks! I can now use printf statements to debug.... I no longer have a Segmentation fault! – Graham May 04 '17 at 16:26
0

char *reverse in toStrong is not allocated any memory, that could lead to segment fault, part from that argv[0] is always name of program you run, you need to use argv[1] onwards for your inputs

Pras
  • 4,047
  • 10
  • 20
  • Not necessarily always. See http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi – ktbiz May 04 '17 at 02:51