简体   繁体   English

如何输入结构的指针

[英]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. 您已经声明了指向struct rational指针,但没有指定它们实际指向任何这样的结构。 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. 您将函数声明为返回struct rational (即struct rational add() ),但它们似乎没有返回任何内容。 if a function does not return anything it should be declared as void - void add() 如果一个函数没有返回任何东西,它应该被声明为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. 从现在开始,p1保存结构1的地址,依此类推。 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: 您的代码已修改为使用指向struct,struct和integer的指针:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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