【问题标题】:Inputting values to structure members using pointers使用指针向结构成员输入值
【发布时间】:2013-04-13 21:23:19
【问题描述】:

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

#include <stdio.h>
struct rational
{
    int nu;
    int de;
}*p1,*p2,*p3;
void 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);
}
void multiply()
{
    p3->nu = p1->nu * p2->nu;
    p3->de = p1->de * p2->de;
    printf("%d\n--\n%d\n",p3->nu,p3->de);
}
void 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,c,d,choice;
    printf("Enter the first rational number.\n");
    scanf("%d%d",&a,&b);
    p1->nu = a;
    p1->de = b;
    printf("Enter the second rational number.\n");
    scanf("%d%d",&c,&d);
    p2->nu = c;
    p2->de = d;
    scanf("%d",&choice);
    switch (choice)
    {
        case 1: add();
                break;
        case 2: multiply();
                break;
        case 3: divide();
                break;
    }
    return 0;
}

【问题讨论】:

    标签: c pointers syntax structure


    【解决方案1】:

    您从不初始化指针,因此您提供的代码会调用未定义的行为。在实际使用 p1 之前,p2 和 p3 让它们指向某个现有对象,或者为它们动态分配内存。

    【讨论】:

    • 如何为他们分配内存?
    • @Shail 使用malloc。谷歌搜索会帮助你知道。
    【解决方案2】:

    例如

    #include <stdio.h>
    #include <stdlib.h>
    
    struct rational{
        int nu;
        int de;
    } ;
    
    int main(void){
        int n, d;
        struct rational *p;
    
        p=(struct rational*)malloc(sizeof(struct rational));
        printf("Enter the rational number.\n");
    /*  Indirection
        scanf("%d/%d", &n, &d);
        p->nu = n;
        p->de = d;
    */
    scanf("%d/%d", &p->nu, &p->de);//Direct
        printf("%d/%d\n", p->nu, p->de);
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      解决了。我在main() 中声明了 3 个struct rational 变量:p1p2p3。然后我将这些变量的地址导出到以三个指针为原型的函数中。

      #include <stdio.h>  
      struct rational
      {
          int nu;
          int de;
      };
      void add(struct rational *p1,struct rational *p2,struct rational *p3)
      {
          p3->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);
      }
      void multiply(struct rational *p1,struct rational *p2,struct rational *p3)
      {
          p3->nu = p1->nu * p2->nu;
          p3->de = p1->de * p2->de;
          printf("%d\n-\n%d\n",p3->nu,p3->de);
      }
      void divide(struct rational *p1,struct rational *p2,struct rational *p3)
      {
          p3->nu = p1->nu * p2->de;
          p3->de = p1->de * p2->nu;
          printf("%d\n-\n%d\n",p3->nu,p3->de);
      }
      int main()
      {
          struct rational p1,p2,p3;
      
          int a,b,c,d,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);
          printf("1. Addition 2. Multiplication 3. Division: ");
          scanf("%d",&choice);
          switch (choice)
          {
              case 1: add(&p1,&p2,&p3);
                      break;
              case 2: multiply(&p1,&p2,&p3);
                      break;
              case 3: divide(&p1,&p2,&p3);
                      break;
          }
          return 0;
      }
      //Output
      //Enter the first rational number.
      //3/4
      //Enter the second rational number.
      //7/9
      //1. Addition 2. Multiplication 3. Division: 1
      //55
      //-
      //36
      

      【讨论】:

        猜你喜欢
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多