c - How to input pointer of a structure -
i writing program finding addition, multiplication , division of 2 rational numbers using structure , pointers. having problem inputting numbers pointers. how should code corrected? thanks!
#include <stdio.h> struct rational { int nu; int de; }*p1,*p2,*p3; struct rational 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); } struct rational multiply() { p3->nu = p1->nu * p2->nu; p3->de = p1->de * p2->de; printf("%d\n--\n%d\n",p3->nu,p3->de); } struct rational 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,choice; printf("enter first rational number.\n"); scanf("%d%d",&p1->nu,&p1->de); printf("enter second rational number.\n"); scanf("%d%d",&p2->nu,&p2->de); scanf("%d",&choice); switch (choice) { case 1: add(); break; case 2: multiply(); break; case 3: divide(); break; } return 0; }
you have declared pointers
struct rational
did not assign them point such struct. example:struct rational rat_a; p1 = & rat_a;
you declare functions returning
struct rational
(i.e.struct rational add()
) don't seem return anything. if function not return should declared void -void add()
why use pointers structs , not structs themselves? (if declared global in code)
Comments
Post a Comment