简体   繁体   中英

How to input pointer of a structure

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;
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 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);
    scanf("%d",&choice);
    switch (choice)
    {
        case 1: add();
                break;
        case 2: multiply();
                break;
        case 3: divide();
                break;
    }
    return 0;
}
  1. You have declared pointers to struct rational but did not assign them to actually point to any such struct. for example:

     struct rational rat_a; p1 = & rat_a; 
  2. You declare your functions as returning struct rational (ie struct rational add() ) but they don't seem to return anything. if a function does not return anything it should be declared as void - void add()

  3. Why do you use pointers to structs and not the structs themselves? (if declared global as in your code)

You declare pointers to the structures, not structures themself. Pointer to structure can hold address of structure but doesn't create one. You must have structure, to make pointer point to it.

struct rational
{
    int nu;
    int de;
}*p1,*p2,*p3;

This part makes 3 pointers to structures. They point at nothing right now. Now you have to create actual structures like

struct rational structure1, structure2, structure3;

Now you can assign value to pointer:

p1=&structure1;
p2=&structure2;
p3=&structure3;

From now on p1 holds the address of structure 1 and so on. And from thin point only you can dereference pointers with

p1->de
p2->nu

What,I think,buried you was making those global pointers. It really looks ugly when function takes nothing and makes something all of the sudden. It would look so much nicer and cleaner if you passed your structures by reference to functions. Next thing, you functions seem to be returning structures but they don't.Decide whether or not you modify them by pointers. Also when you define function that doesn't take arguments you do it like

int funct(void);

In your case functions don't take arguments and dont return value so every one of them should look like this:

void myfunction(void);

Your code modified to use pointer to struct, struct and integers:

#include <stdio.h>
typedef struct 
{
    int nu;
    int de;
}rational;

rational *p1, elements;

int add(rational *p4)
{
    int a;
    a = p4->nu+p4->de;

    return a;
}
int multiply(rational *p4)
{
    int a;
    a = p4->de * p4->nu;

    return a;
}
int divide(rational *p4) //should return a float instead of int to show decimal returns
{
    int a;
    a = p4->nu / p4->de;

    return a;
}
int main()
{
    int a,b,choice;

    p1 = &elements;

    printf("Enter the first rational number.\n");
    scanf("%d",&a);
    printf("Enter the second rational number.\n");
    scanf("%d",&b);
    printf("Enter choice /(1, 2 or 3/).\n");

    scanf("%d",&choice);

    p1->nu = a;
    p1->de = b;
    switch (choice)
    {
        case 1: 
            a = add(p1);
            printf("%d + %d = %d\n",p1->nu, p1->de, a);

                break;
        case 2: 
            a= multiply(p1);
                printf("%d * %d = %d\n",p1->nu, p1->de, a);

                break;
        case 3: 
            a = divide(p1);
            printf("%d / %d = %d\n",p1->nu, p1->de, a);
                break;
    }
    getchar();//eats an additional character?...
    getchar();//stop execution so you can see your answer
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM