变量'c'和'd'不必是通过引用传递它们的指针。因此,您有两种情况:
-
在主函数中将'c'和'd'定义为指针时,您将把它们传递给以下函数:pointerIntro(first,second,c,d);
,因为它们已经是指针了,您不需要发送它们的引用,您只需发送给他们。
-
如果将“ c”和“ d”定义为双变量double c,d;
,则将使用“&”符号通过引用将它们发送给函数,例如:pointerIntro(first,&c,&d);
。
p>
然后在您的函数中实际设置'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,d;
scanf("%lf",&a);
scanf("%lf",&b);
myFunction(a,&d);
printf("%lf %lf",d);
}
,
您需要取消引用c
和d
才能在myFunction()
中进行分配:
*c = a/b;
*d = a*b;
此外,您还需要创建实例,其中c
和d
指向以下实例:
double c;
double d;
(您创建了没有实例的统一指针)。
然后在通话中:
pointerIntro(first,&d);
&
地址操作符创建 reference 自变量,引用c
中的d
和main()
。
不要在c
和d
中使用相同的符号名称main()
和myFunction()
来使它们更清楚,这可能是有用的。例如:
void myFunction(double a,double* cptr,double* dptr )
{
*cptr = a/b;
*dptr = a*b;
}
还请注意,应该声明void
。不返回任何内容的函数。
,
指针只是一个普通变量,它以其他值作为地址。换句话说,指针指向可以找到其他内容的地址。在通常情况下,您会想到一个包含立即数的变量,例如int a = 40;
,而指针(例如int *p = &a;
)将仅保存40
存储在内存中的地址。
如果您需要访问存储在p
指向的内存地址中的值,请使用一元p
运算符,例如取消引用 '*'
。 int j = *p;
将初始化j = 40
)。
如果要获取内存中的变量地址,请使用一元'&'
( 的地址)运算符。如果需要将变量作为指针传递,只需提供变量的地址作为参数。
在您的情况下,归结为编写类似于以下内容的函数:
void multdiv (double a,double *d)
{
*c = a / b;
*d = a * b;
}
(注意:在划分b != 0
之前,应确保a / b
留给您)
为了存储将用作输入的值以及要保存乘法和除法结果的值,您需要四个double
值,例如
int main (void) {
double a,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;
}
(奖金:为什么'&'
和a
前的b
使用scanf
?请参见下的第一个完整段落man 3 scanf
中的description )
剩下的就是调用您的函数来执行计算并输出结果,例如:
multdiv (a,&d); /* calculate c,d */
printf ("\nc = a / b => %.2f\nd = a * b => %.2f\n",d);
(注意:如上文所述,运算符的地址用于传递 c
和{{1 }}作为参数)
完全将其放入您可以做的事情:
d
使用/输出示例
#include <stdio.h>
void multdiv (double a,double *d)
{
*c = a / b;
*d = a * b;
}
int main (void) {
double a,d; /* your doubles */
fputs ("enter two double values: ",stderr);
return 1;
}
multdiv (a,d);
return 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,*z,*w);
}
main()
方法
int main(void){
double first,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,&val1,&val2);
}
,
-
这是完全有效的。您可以初始化并传递任意数量的指针变量及其引用。
-
这也是有效的。当您传递变量地址时,应将其存储到指针中
您必须对代码进行一些更改,
您可以直接分配%lf
指针变量#include <stdio.h>
#include <string.h>
void myFunction(double a,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,&d);
}
然后,您必须使用complete
格式参数读取双数。
subscribe
本文链接:https://www.f2er.com/3090937.html