C简介-如何通过引用在函数中传递参数?

问题描述 投票:1回答:4

我正在从事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 function pointers pass-by-reference
4个回答
1
投票
  1. 完全正确。您可以初始化并传递任意数量的指针变量及其引用。

  2. 这也是有效的。当您传递变量地址时,应将其存储到指针中

您必须对代码进行一些更改,您可以直接分配a/b and a*b指针变量*c & *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);
}

1
投票

变量'c'和'd'不必是通过引用传递它们的指针。因此,您有两种情况:

  1. 当您在主函数中将'c'和'd'定义为指针时,您将把它们传递给如下函数:pointerIntro(first, second, c, d);因为它们已经是指针并且您不需要发送它们的引用,您只需发送他们。

  2. 如果将'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);
}

0
投票

这里是正确的方法。 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);  
}

0
投票

您需要取消引用cd才能在myFunction()中进行分配:

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

此外,您还需要创建instances,其中cd所指:

double c;
double d;

((您创建了没有实例的统一指针)。

然后在通话中:

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

&地址的运算符创建reference自变量,引用c中的dmain()

[不要在cd中使用相同的符号名称main()myFunction()以使它们更清楚,这很有用。例如:

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

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


-1
投票

您应该在这里了解到,您仅声明了指针。您尚未分配任何空间。

  double *c;
  double *d;

您可以使用allocate / malloccalloc空格。例如

c = malloc(sizeof(double));
d = malloc(sizeof(double));

或者您可以同时声明和分配空间,例如

double *c = malloc(sizeof(double));
double *d = malloc(sizeof(double));

第二个问题在函数定义内。您正在将结果分配给指针,这是错误的。您必须将结果放置在指针指向的位置。正确的方法是

*c = a/b;    /* Store the results at location pointed to by variable c */
*d = a*b;    /* Store the results at location pointed to by variable d */
© www.soinside.com 2019 - 2024. All rights reserved.