C 简介 - 如何在函数中通过引用传递参数?
C intro - How to pass a parameter by reference in function?
我正在完成我的介绍性 C 课程作业,我的任务是以下...
- 为函数编写代码,该函数按值接收两个参数(a 和 b)并按引用接收另外两个参数(c 和 d)。所有参数都是double.
- 从 main 开始,使用 scanf 获取两个数字,然后调用该函数,然后将两个返回值显示到 printf 语句的输出中。
- 该函数通过将 (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
和 d
以在 myFunction()
:
中分配它们
*c = a/b;
*d = a*b;
此外,您需要创建实例,c
和d
指的是:
double c;
double d;
(您创建了没有实例的单元化指针)。
然后在通话中:
pointerIntro(first, second, &c, &d);
&
address-of 运算符创建 reference 参数,引用 main()
中的 c
和 d
。
在 main()
和 myFunction()
中不要使用相同的符号名称 c
和 d
可能会有用,这样可以更清楚地说明它们是什么。例如:
void myFunction(double a, double b, double* cptr, double* dptr )
{
*cptr = a/b;
*dptr = a*b;
}
另请注意,returns 不应声明任何内容的函数 void
。
这是正确的做法。 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);
}
变量 'c' 和 'd' 不必是指针即可通过引用传递它们。所以你有两种情况:
当您在 main 函数中将 'c' 和 'd' 定义为指针时,您将像这样将它们传递给函数:pointerIntro(first, second, c, d);
因为它们已经是指针而您不需要发送他们的推荐信,您只需发送他们即可。
如果您将 '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);
}
完全正确。您可以使用它们的引用初始化和传递任意数量的指针变量。
这也是有效的..当你传递变量地址时,你应该将它存储到一个指针中
您必须对代码进行一些更改,
可以直接赋值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);
}
指针只是一个普通变量,它保存地址 其他东西作为它的值。换句话说,指针 指向 可以找到其他内容的地址。在您通常认为变量保存立即值的地方,例如 int a = 40;
,指针(例如 int *p = &a;
)将简单地保存内存中存储 40
的地址。
如果您需要访问存储在 p
指向的内存地址的值,您 解引用 p
使用一元 '*'
运算符,例如int j = *p;
将初始化 j = 40
).
如果要获取内存中的变量地址,可以使用一元'&'
(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 */
然后您需要提示用户输入并验证用户输入 检查所用输入函数的return,例如
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: 为什么在a
和b
之前用'&'
和上面的scanf
?先看全DESCRIPTION 下的段落 man 3 scanf
)
剩下的就是调用你的函数来执行计算并输出结果,例如:
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
(注:如上解释address of运算符用于传递address of c
和 d
作为参数)
总而言之你可以做到:
#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;
}
例子Use/Output
$ ./bin/multdivbyptrs
enter two double values: 4.0 2.0
c = a / b => 2.00
d = a * b => 8.00
检查一下,如果您有任何问题,请告诉我。一旦你理解了指针只是一个普通变量,它保存了存储其他东西的地址作为它的值——事情就会开始落实到位。没有魔法..
我正在完成我的介绍性 C 课程作业,我的任务是以下...
- 为函数编写代码,该函数按值接收两个参数(a 和 b)并按引用接收另外两个参数(c 和 d)。所有参数都是double.
- 从 main 开始,使用 scanf 获取两个数字,然后调用该函数,然后将两个返回值显示到 printf 语句的输出中。
- 该函数通过将 (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
和 d
以在 myFunction()
:
*c = a/b;
*d = a*b;
此外,您需要创建实例,c
和d
指的是:
double c;
double d;
(您创建了没有实例的单元化指针)。
然后在通话中:
pointerIntro(first, second, &c, &d);
&
address-of 运算符创建 reference 参数,引用 main()
中的 c
和 d
。
在 main()
和 myFunction()
中不要使用相同的符号名称 c
和 d
可能会有用,这样可以更清楚地说明它们是什么。例如:
void myFunction(double a, double b, double* cptr, double* dptr )
{
*cptr = a/b;
*dptr = a*b;
}
另请注意,returns 不应声明任何内容的函数 void
。
这是正确的做法。 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);
}
变量 'c' 和 'd' 不必是指针即可通过引用传递它们。所以你有两种情况:
当您在 main 函数中将 'c' 和 'd' 定义为指针时,您将像这样将它们传递给函数:
pointerIntro(first, second, c, d);
因为它们已经是指针而您不需要发送他们的推荐信,您只需发送他们即可。如果您将 '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);
}
完全正确。您可以使用它们的引用初始化和传递任意数量的指针变量。
这也是有效的..当你传递变量地址时,你应该将它存储到一个指针中
您必须对代码进行一些更改,
可以直接赋值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);
}
指针只是一个普通变量,它保存地址 其他东西作为它的值。换句话说,指针 指向 可以找到其他内容的地址。在您通常认为变量保存立即值的地方,例如 int a = 40;
,指针(例如 int *p = &a;
)将简单地保存内存中存储 40
的地址。
如果您需要访问存储在 p
指向的内存地址的值,您 解引用 p
使用一元 '*'
运算符,例如int j = *p;
将初始化 j = 40
).
如果要获取内存中的变量地址,可以使用一元'&'
(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 */
然后您需要提示用户输入并验证用户输入 检查所用输入函数的return,例如
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: 为什么在a
和b
之前用'&'
和上面的scanf
?先看全DESCRIPTION 下的段落 man 3 scanf
)
剩下的就是调用你的函数来执行计算并输出结果,例如:
multdiv (a, b, &c, &d); /* calculate c, d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n", c, d);
(注:如上解释address of运算符用于传递address of c
和 d
作为参数)
总而言之你可以做到:
#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;
}
例子Use/Output
$ ./bin/multdivbyptrs
enter two double values: 4.0 2.0
c = a / b => 2.00
d = a * b => 8.00
检查一下,如果您有任何问题,请告诉我。一旦你理解了指针只是一个普通变量,它保存了存储其他东西的地址作为它的值——事情就会开始落实到位。没有魔法..