【问题标题】:C intro - How to pass a parameter by reference in function?C 介绍 - 如何在函数中通过引用传递参数?
【发布时间】:2019-11-16 08:43:25
【问题描述】:

我正在完成我的 C 入门课程作业,我的任务是完成以下任务...

  1. 为一个函数编写代码,该函数通过值接收两个参数(a 和 b)并通过引用具有另外两个参数(c 和 d)。所有参数都是双精度的。
  2. 从 main 中,使用 scanf 获取两个数字,然后调用该函数,然后在 printf 语句中将两个返回值显示到输出。
  3. 该函数通过将 (a/b) 分配给 c 并将 (a*b) 分配给 d 来工作。

虽然我的知识很基础,但我相信我了解要点 在主要

      //first and second double hold the scanf inputs
      double first;
      double second;

      //unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
      double *c;
      double *d;

   printf("Enter your first number\n");
   scanf("%f\n", &first);
   printf("Enter your second number\n");
   scanf("%d\n", &second);

   //call the function, first and second by value, &c / &d by reference - correct?
   pointerIntro(first, second, &c, &d);

对于函数...

float myFunction(double a, double b, double *c, double *d)
{
c = a/b;
d = a*b;


//printf statements
}

如果这个问题的流程很混乱,我深表歉意,但它对我来说是过程的一部分:P

所以,对于我的正式问题 1.在main中启动两个双指针变量(*c和*d)作为函数中的引用传递是否正确? 2. 我可以通过说 &c / &d 来调用带有引用指针的函数吗? 3. 对这个提问还有其他批评吗?

【问题讨论】:

  • 在 C 中,失去“按引用”的白话。使用“按地址”。从长远来看,它会让你的生活更轻松;相信我。仅供参考,main 中的 cd 应该是常规的 double,而不是 double *。此外,在您的函数*c = a/b; 和后续行中的类似内容可能是您的意图。你所拥有的至少应该发出一个关于将int 分配给int * 类型的警告。如果它没有出现您的警告,则将其视为错误。
  • 是的,C 简介。这个问题可能措辞不当。
  • "所有参数都是双精度的。" 没有。两个是double,两个是指向double的指针,即double* .
  • 如果你想在调用函数中存储 four double 你需要定义 four double.
  • @OznOg et al “通过引用传递”的行为是一个独立于语言的通用计算机科学术语。 C 不支持显式 引用类型 的事实不会使术语 “通过引用” 在这种情况下无效 - 在 C++ 成为事物之前它是常用术语。

标签: c function pointers pass-by-reference


【解决方案1】:

变量 'c' 和 'd' 不必是通过引用传递它们的指针。所以你有两种情况:

    1234563只需发送它们。
  1. 如果您将“c”和“d”定义为双变量double c, d;,您将使用“&”符号通过引用将它们发送给函数,如下所示:pointerIntro(first, second, &c, &d);

    李>

然后在你的函数中实际设置'c'和'd'的值,你需要像这样取消引用指向它们的指针:*c = a/b; *d = a*b;

如果您不熟悉,可以在此处查看取消引用指针的含义:What does "dereferencing" a pointer mean?

应该可以工作的代码:

#include <stdio.h>

void myFunction(double a, double b, double *c, double *d)
{
    *c = a / b;
    *d = a * b;
}

int main(void)
{
    double a, b, c, d;

    scanf("%lf", &a);
    scanf("%lf", &b);

    myFunction(a, b, &c, &d);

    printf("%lf %lf", c, d);
}

【讨论】:

  • 采用这种可能的解决方案,将 main 中的 c 和 d 设置为普通双精度,在函数调用中用 & 引用它们,并在函数逻辑中使用 *,我遇到了这个错误 - “间接需要指针操作数('double' 无效)"
  • 首先我忽略了你的函数有浮点返回类型,但它没有返回任何东西,所以也要小心,我自己运行了代码,它工作了。我将尝试找出该错误消息的原因,如果发现任何问题,我会通知您。
  • @JonGardocki :这是一个很好的答案,比您目前接受的有缺陷的答案要好得多(我的也很好;-))。提供的代码编译没有错误,因此您需要考虑它与您所拥有的代码有何不同。错误不在于这个答案。
【解决方案2】:

您需要取消引用cd 以将它们分配到myFunction()

*c = a/b;
*d = a*b;

此外,您需要从 main 创建 instancescd 引用:

double c;
double d;

(您已经创建了没有实例的未初始化指针)。

然后在调用中:

pointerIntro(first, second, &c, &d);

&amp; 地址操作符创建 reference 参数,引用main() 中的cd

不要在main()myFunction() 中使用相同的符号名称cd 以更清楚地说明它们是什么。例如:

void myFunction(double a, double b, double* cptr, double* dptr )
{
    *cptr = a/b;
    *dptr = a*b;
}

另请注意,不返回任何内容的函数应声明为void

【讨论】:

    【解决方案3】:

    指针只是一个普通变量,它保存着其他东西的地址作为它的值。换句话说,一个指针指向可以找到其他东西的地址。通常你会想到一个保存立即数的变量,例如int a = 40;,而指针(例如int *p = &amp;a;)只会保存40 存储在内存中的地址。

    如果您需要访问存储在p 指向的内存地址中的值,您取消引用 p 使用一元'*' 运算符,例如int j = *p; 将初始化 j = 40)。

    如果要获取内存中的变量地址,请使用一元 '&amp;' (address of) 运算符。如果您需要将变量作为指针传递,您只需提供变量的地址作为参数。

    在您的情况下,归结为编写类似于以下内容的函数:

    void multdiv (double a, double b, double *c, double *d)
    {
        *c = a / b;
        *d = a * b;
    }
    

    注意:你应该在划分a / b之前确保b != 0——这留给你)

    为了存储您将作为输入的值以及要保存乘法和除法结果的值,您需要四个 double 值,例如

    int main (void) {
    
        double a, b, c, d;          /* your doubles */
    

    然后您需要提示用户输入并通过检查返回验证用户输入使用的输入函数,例如

        fputs ("enter two double values: ", stdout);        /* prompt */
        fflush (stdout);    /* flush stream (optional but recommended) */
    
        if (scanf ("%lf %lf", &a, &b) != 2) {   /* validate EVERY input */
            fputs ("error: invalid double values.\n", stderr);
            return 1;
        }
    

    (bonus: 为什么'&amp;'ab 之前使用,上面有scanf?请参阅@987654321 中DESCRIPTION 下的第一个完整段落@)

    剩下的就是调用你的函数来执行计算并输出结果,例如:

        multdiv (a, b, &c, &d);     /* calculate c, d */
    
        printf ("\nc = a / b  => %.2f\nd = a * b  => %.2f\n", c, d);
    

    (注意: 如上所述,address of 运算符用于将cd地址作为参数传递)

    总之你可以做到:

    #include <stdio.h>
    
    void multdiv (double a, double b, double *c, double *d)
    {
        *c = a / b;
        *d = a * b;
    }
    
    int main (void) {
    
        double a, b, c, d;          /* your doubles */
    
        fputs ("enter two double values: ", stdout);        /* prompt */
        fflush (stdout);    /* flush stream (optional but recommended) */
    
        if (scanf ("%lf %lf", &a, &b) != 2) {   /* validate EVERY input */
            fputs ("error: invalid double values.\n", stderr);
            return 1;
        }
    
        multdiv (a, b, &c, &d);     /* calculate c, d */
    
        printf ("\nc = a / b  => %.2f\nd = a * b  => %.2f\n", c, d);
    
        return 0;
    }
    

    使用/输出示例

    $ ./bin/multdivbyptrs
    enter two double values: 4.0 2.0
    
    c = a / b  => 2.00
    d = a * b  => 8.00
    

    检查一下,如果您有任何问题,请告诉我。一旦你理解指针只是一个普通变量,它保存存储其他东西的地址作为它的值 - 事情就会开始到位。没有魔法..

    【讨论】:

      【解决方案4】:
      1. 这是完全有效的。您可以初始化并传递任意数量的指针变量及其引用。

      2. 这也是有效的..当你传递变量地址时,你应该将它存储到一个指针中

      您必须对代码进行一些更改, 可以直接赋值a/b and a*b指针变量*c &amp; *d 然后你必须使用%lf 格式参数读取双数。

      #include <stdio.h>
      #include <string.h>
      
      void myFunction(double a, double b, double *c, double *d)
      {
      *c = a/b;   //change
      *d = a*b;   //change
      printf("%lf %lf",*c,*d);
      return;
      //printf statements
      }
      
      
      
      int main()
      {
      //first and second double hold the scanf inputs
      double first;
      double second;
      
      //unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
      double *c;
      double *d;
      
      printf("Enter your first number\n");
      scanf("%lf", &first);   //change
      printf("Enter your second number\n");
      scanf("%lf", &second);  //change
      
      //call the function, first and second by value, &c / &d by reference - correct?
      myFunction(first, second, &c,&d);
      }
      

      【讨论】:

      • 使用此代码,我收到“控制到达非无效函数的结尾”错误。在函数声明中。有什么想法吗?
      • 请将返回类型更改为void,因为您不需要返回任何内容对吗??
      • 正确。非常感谢你,VJAY - 只需要按照我的方式完成每一步,但你的解决方案对我的帮助很大。谢谢。
      • @JonGardocki 警告这不是一个完整的答案,而且目前存在严重缺陷。您需要将main() 中的cd 更改为instances 而不是pointersmyFunction(first, second, &amp;c,&amp;d);cd 已经是double* (并且未初始化)。也许考虑其他答案。
      【解决方案5】:

      这是正确的方法。 func() 方法

      void func(double x,double y,double *z,double *w){
          printf("pass by value = %lf, %lf",x,y);
          printf("pass by reference = %lf, %lf",*z,*w);
      }
      

      main() 方法

      int main(void){
         double first,second,val1,val2;
         val1=3.1;
         val2=6.3;
         printf("Enter your first number\n");
         scanf("%lf", &first);
         printf("Enter your second number\n");
         scanf("%lf", &second);
         func(first,second,&val1,&val2);  
      }
      

      【讨论】:

        猜你喜欢
        • 2019-02-12
        • 1970-01-01
        • 2018-06-26
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多