Reputation: 901
I was writing a program for finding the addition, multiplication and division of two rational numbers using a structure and pointers. I am having a problem inputting the numbers with pointers. How should my code be corrected? Thanks!
#include <stdio.h>
struct rational
{
int nu;
int de;
}*p1,*p2,*p3;
void add()
{
p1->nu = p1->nu*p2->de + p1->de*p2->nu;
p3->de = p1->de * p2->de;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
void multiply()
{
p3->nu = p1->nu * p2->nu;
p3->de = p1->de * p2->de;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
void divide()
{
p3->nu = p1->nu * p2->de;
p3->de = p1->de * p2->nu;
printf("%d\n--\n%d\n",p3->nu,p3->de);
}
int main()
{
int a,b,c,d,choice;
printf("Enter the first rational number.\n");
scanf("%d%d",&a,&b);
p1->nu = a;
p1->de = b;
printf("Enter the second rational number.\n");
scanf("%d%d",&c,&d);
p2->nu = c;
p2->de = d;
scanf("%d",&choice);
switch (choice)
{
case 1: add();
break;
case 2: multiply();
break;
case 3: divide();
break;
}
return 0;
}
Upvotes: 0
Views: 243
Reputation: 901
Solved. I declared 3 struct rational
variables: p1
,p2
and p3
in the main()
. Then I exported the addresses of those variable to the functions which had three pointers as their prototypes.
#include <stdio.h>
struct rational
{
int nu;
int de;
};
void add(struct rational *p1,struct rational *p2,struct rational *p3)
{
p3->nu = (p1->nu)*(p2->de) + (p1->de)*(p2->nu);
p3->de = p1->de * p2->de;
printf("%d\n-\n%d\n",p3->nu,p3->de);
}
void multiply(struct rational *p1,struct rational *p2,struct rational *p3)
{
p3->nu = p1->nu * p2->nu;
p3->de = p1->de * p2->de;
printf("%d\n-\n%d\n",p3->nu,p3->de);
}
void divide(struct rational *p1,struct rational *p2,struct rational *p3)
{
p3->nu = p1->nu * p2->de;
p3->de = p1->de * p2->nu;
printf("%d\n-\n%d\n",p3->nu,p3->de);
}
int main()
{
struct rational p1,p2,p3;
int a,b,c,d,choice;
printf("Enter the first rational number.\n");
scanf("%d/%d", &p1.nu, &p1.de);
printf("Enter the second rational number.\n");
scanf("%d/%d", &p2.nu, &p2.de);
printf("1. Addition 2. Multiplication 3. Division: ");
scanf("%d",&choice);
switch (choice)
{
case 1: add(&p1,&p2,&p3);
break;
case 2: multiply(&p1,&p2,&p3);
break;
case 3: divide(&p1,&p2,&p3);
break;
}
return 0;
}
//Output
//Enter the first rational number.
//3/4
//Enter the second rational number.
//7/9
//1. Addition 2. Multiplication 3. Division: 1
//55
//-
//36
Upvotes: 0
Reputation: 40155
E.g.
#include <stdio.h>
#include <stdlib.h>
struct rational{
int nu;
int de;
} ;
int main(void){
int n, d;
struct rational *p;
p=(struct rational*)malloc(sizeof(struct rational));
printf("Enter the rational number.\n");
/* Indirection
scanf("%d/%d", &n, &d);
p->nu = n;
p->de = d;
*/
scanf("%d/%d", &p->nu, &p->de);//Direct
printf("%d/%d\n", p->nu, p->de);
return 0;
}
Upvotes: 1
Reputation: 70989
You never initialize the pointers so the code you provide is invoking undefined behavior. Before actually using p1, p2 and p3 have them pointing to some existing object or alternatively allocate memory for them dynamically.
Upvotes: 1