Pointers to structs

This is a discussion on Pointers to structs within the C Programming forums, part of the General Programming Boards category; I have this program to write that will add two fractions input by the user. I need to do this ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Pointers to structs

    I have this program to write that will add two fractions input by the user. I need to do this using pointers to structs. I have the code written, but it will not run all the way through. Could someone help me debug this thing and get it running properly.

    #include <stdio.h>

    typedef struct
    {
    int numerator;
    int denominator;
    }FRACTION;

    void Add(FRACTION *pfrac1, FRACTION *pfrac2);
    void Display(FRACTION *pres);

    int main()
    {
    FRACTION fr1;
    FRACTION fr2;
    FRACTION result;

    FRACTION *pfrac1;
    FRACTION *pfrac2;
    FRACTION *pres;

    pfrac1 = &fr1;
    pfrac2 = &fr2;
    pres = &result;

    printf("Please enter the first fraction in the form x/y: ");
    scanf("%d /%d", pfrac1->numerator, pfrac2->denominator);
    printf("Please enter the second fraction in the form x/y: ");
    scanf("%d /%d", pfrac2->numerator, pfrac2->denominator);
    Add(&fr1, &fr2);
    Display(&result);
    getchar();
    getchar();
    }

    void Add(FRACTION *pfrac1, FRACTION *pfrac2)
    {
    FRACTION result;
    FRACTION *pres;

    pres = &result;

    pres->numerator = (pfrac1->numerator * pfrac2->denominator) + (pfrac2->numerator * pfrac1->denominator);
    pres->denominator = (pfrac1->denominator * pfrac2->denominator);
    }
    void Display(FRACTION *pres)
    {
    printf("\nThe result is %d/%d", pres->numerator, pres->denominator);
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Addendum to pointers to structs

    I would just like to add that I would also like to omit the statements in "main" to obtain the input from user and create a function called "GetInput" that would handle that so that "main" doesnt look "messy" if you will.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need the address of numerator and denominator:

    scanf("%d /%d", &pfrac1->numerator, &pfrac2->denominator);

    Also use code tags when posting code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. pointers and structs
    By alexir in forum C Programming
    Replies: 0
    Last Post: 10-20-2001, 01:28 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21