1

I made a program where I want to multiply big integers which are actually bigger than the default integer, and we had to handle this with a string of the number which we should turn into an int array. Now we have to multiply a big number with a number below 10. I tried this multiplication and on paper it all worked but now when I try the program it's weird because I get the message "Segmentation fault (core dumped)". What does it mean and how can i fix it? My main function:

    int main(int argc, char *argv[])
{
    struct BigInt firstNumber;
    struct BigInt result;
    char userInput[2];
    printf("Pyramid of numbers\n\n");
    printf("Please enter a number: " );
    scanf("%s",userInput );
    int len=strlen(userInput);
    len=strtobig_int(userInput, len, &firstNumber);
    firstNumber.digits_count=len;
    print_big_int(&firstNumber);
    printf("\n%d\n",len);
    multiply(&firstNumber, 5, &result);
    print_big_int(&firstNumber);
    printf("\n\n");
    print_big_int(&result);
    return 0;
 }

My multiply function:

void multiply(const struct BigInt *big_int, int factor, struct BigInt *big_result){
    int overflowNumber=0;
    for (int i = big_int->digits_count-1 ; i >0; i++) {          //for loop which counts from the end of the integer to the beginning, like we'd do it in reallife
        int tempResult=big_int->the_int[i]*factor;                              //tempResult is the temporary Result
        if (tempResult>9) {                                                                     //here we check if the tempResult is bigger than 9, because if it is, we'd have to split the two parts (e.g. 1 and 4 for 14) and put 1 to the next
                big_result->the_int[i]=tempResult%10+overflowNumber;
                overflowNumber=tempResult/10;
        }else{
            big_result->the_int[i]=tempResult+overflowNumber;
        }
    }
}

0 Answers0