C language only.
I get the error message at line 7 when I run this code:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
Also, I get the error message at line 8:
warining: function returns address of local variable [-Wreturn-local-addr]
I have attached a picture of what I am trying to output. Could someone please show the corrected code, it would be much appreciated, thanks!
#include <stdio.h>
// Method getFraction() asks for numerator and denominator
int *getFraction() {
int *n[2]; // declare array of pointers to return the numerator and
// denominator
printf("\n Enter the numerator and the denominator -- ");
scanf("%d%d", (n 0), (n 1)); // read numerator and denominator
return n; // return n
}
// method smallest() takes twointegers as its parameter and return the smallest
int smallest(int a, int b) {
if (a < b) // condition for a is smallest
return a; // return a
else
return b; // return b
}
// recursive method to return the GCD() of two numbers
int gcd(int a, int b) {
if (b == 0) // if denominator is 0 then return a
return a;
// recursively call to gcd()
return gcd(b, a % b);
}
// method reduce() takes two parameters as call by reference and
// reduce the fraction
void reduce(int *a, int *b) {
int g;
// call to gcd()
g = gcd(*a, *b);
// reduce numerator
*a = *a / g;
// reduce denominator
*b = *b / g;
}
// driver code
int main() {
int *num, a, b, small, g;
char ch;
// loop to repeat the process till user wants
do {
num = getFraction(); // call to getFraction();
a = *(num 0); // assign numerator to a
b = *(num 1); // assign denominator to b
// condition for denominator not zero
if (b != 0) {
reduce(&a, &b); // call to reduce() as call by reference
// print the reduced fraction
printf("\n The reduced fraction is -- %d / %d", a, b);
}
else printf("\n Denominator should not be zero.");
// ask user to repeat more
printf("\n Try again (Y/N) -- ");
fflush(stdin);
scanf("%c", &ch); // read users choice
} while (ch == 'y' || ch == 'Y');
return 0;
}
CodePudding user response:
You get an error on line 8 because after your function return its value stack pop local variables and after that we can't access to their addresses and because of that the thing that you return may cause killed with your operating system. compiler error it while build it.
CodePudding user response:
#include <stdio.h>
// Method getFraction() asks for numerator and denominator
int *getFraction() {
int n[2];// declare array of pointers to return the numerator and
// denominator
int *n1=n;
printf("\n Enter the numerator and the denominator -- ");
scanf("%d%d", &n[0], &n[1]); // read numerator and denominator
return n1; // return n
}
// method smallest() takes twointegers as its parameter and return the smallest
int smallest(int a, int b) {
if (a < b) // condition for a is smallest
return a; // return a
else
return b; // return b
}
// recursive method to return the GCD() of two numbers
int gcd(int a, int b) {
if (b == 0) // if denominator is 0 then return a
return a;
// recursively call to gcd()
return gcd(b, a % b);
}
// method reduce() takes two parameters as call by reference and
// reduce the fraction
void reduce(int *a, int *b) {
int g;
// call to gcd()
g = gcd(*a, *b);
// reduce numerator
*a = *a / g;
// reduce denominator
*b = *b / g;
}
// driver code
int main() {
int *num, a, b, small, g;
char ch;
// loop to repeat the process till user wants
do {
num = getFraction(); // call to getFraction();
a = *(num 0); // assign numerator to a
b = *(num 1); // assign denominator to b
// condition for denominator not zero
if (b != 0) {
reduce(&a, &b); // call to reduce() as call by reference
// print the reduced fraction
printf("\n The reduced fraction is -- %d / %d", a, b);
}
else printf("\n Denominator should not be zero.");
// ask user to repeat more
printf("\n Try again (Y/N) -- ");
fflush(stdin);
scanf("%c", &ch); // read users choice
} while (ch == 'y' || ch == 'Y');
return 0;
}