-3

The program is supposed to do integer division and show the remainder. It should check the if the denominator is 0 and allow the user to correct it. However, when the denominator is 0 and a non-zero integer is re-entered, I get a segmentation fault. Could someone explain why and how I can rectify it. I also get a number of warnings. I am kind of new to using pointers.

Here is my code (C):

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

int main(int argc, char const *argv[])
{
    
    int n, d, den, rem;
    int *dvd = &den;
    int *rmn = &rem;


    printf("Enter the numerator: ");  //prompt a user to enter an integer numerator
    scanf("%i", &n);
    printf("%i\n", n);
    printf("Enter the denominator: ");//prompt the user to enter an integer denominator
    scanf("%i", &d);
    printf("%i\n", d);    


    int division (int numerator, int denominator, int *dividend, int *remainder) {
        
          while (denominator==0) {
            printf("Number cannot be 0!\n");
            printf("Enter another number ");
            scanf("%i", denominator);
          }
        
        dividend = numerator/denominator;
        remainder = numerator%denominator;
        printf("%i/%i = %i with %i remainder\n",n, d, dividend, remainder);

    }

    division(n, d, *dvd, *rmn); // call the function division

    
    return 0;
}

I have tried using the address of the denominator instead of the pointer which did not work. I also tried nesting the while loop in an if function.

  • 1
    This code doesn't compile (even after fixing the weird backslashes) so I don't see how it can segfault. You cannot define a function inside another function in C. – pmacfarlane Nov 01 '22 at 18:21
  • 1
    @pmacfarlane: GCC [allows nested function declarations as an extension](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html). Obviously not standard C, but `gcc` could probably compile it. – ShadowRanger Nov 01 '22 at 18:29
  • 1
    Typo: `scanf("%i", denominator);` should be `scanf("%i", &denominator);` like you did for the original input of `0`. Please deal with the warnings you were given. If you don't understand them, please ask a question about them. – Weather Vane Nov 01 '22 at 18:34
  • 1
    Please provide a [mcve]. This one doesn't seem reproducible (doesn't compile -> can't segfault).. – hyde Nov 01 '22 at 18:39
  • There are other pointer errors too, this `dividend = numerator/denominator;` should be `*dividend = numerator/denominator;` and the same for the next `remainder =`. If your problem is with pointers, try to reduce the example code to a single usage. – Weather Vane Nov 01 '22 at 19:02

1 Answers1

0

Call the division function with division(n, d, dvd, rmn); and inside the function replace the uses of divident and remainder with *divident and *remainder.

void division (int numerator, int denominator, int *dividend, int *remainder) {   
    while (denominator==0) {
        printf("Number cannot be 0!\n");
        printf("Enter another number ");
        scanf("%i", denominator);
    }
    *dividend = numerator/denominator;
    *remainder = numerator%denominator;
    printf("%i/%i = %i with %i remainder\n",n, d, *dividend, *remainder);
}
Tenobaal
  • 637
  • 1
  • 16