繁体   English   中英

如何输入结构的指针

[英]How to input pointer of a structure

我正在编写一个程序,用于使用结构和指针查找两个有理数的加法,乘法和除法。 我在使用指针输入数字时遇到问题。 我的代码应该如何纠正? 谢谢!

#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. 您已经声明了指向struct rational指针,但没有指定它们实际指向任何这样的结构。 例如:

     struct rational rat_a; p1 = & rat_a; 
  2. 您将函数声明为返回struct rational (即struct rational add() ),但它们似乎没有返回任何内容。 如果一个函数没有返回任何东西,它应该被声明为void - void add()

  3. 为什么使用指向结构的指针而不是结构本身? (如果在代码中声明为全局)

你声明指向结构的指针,而不是自己构造。 指向结构的指针可以保存结构的地址,但不能创建结构地址。 你必须有结构,使指针指向它。

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

这部分指向结构的三个指针。 他们现在什么也没说。 现在你必须创建像这样的实际结构

struct rational structure1, structure2, structure3;

现在您可以为指针赋值:

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

从现在开始,p1保存结构1的地址,依此类推。 从细小的角度来看,只有你可以解释指针

p1->de
p2->nu

我认为,埋葬你的是制作那些全球性的指针。 当函数什么都不做并突然发生变化时,它看起来真的很难看。 如果通过引用函数传递结构,它看起来会更好,更清晰。 接下来,你的函数似乎是返回结构,但它们没有。决定你是否用指针修改它们。 此外,当您定义不带参数的函数时,您也可以这样做

int funct(void);

在你的情况下,函数不接受参数并且不返回值,因此它们中的每一个都应该如下所示:

void myfunction(void);

您的代码已修改为使用指向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