Reference 在这个C程序中,如何使用引用调用而不是值调用?

Reference 在这个C程序中,如何使用引用调用而不是值调用?,reference,call,Reference,Call,您的第一个程序在许多地方使用引用调用,但也有许多错误 1) 在调用printAll时使用CBR不正确。具体而言,这一行: \asn44.c||In function 'printAll':| \asn44.c|42|error: incompatible type for argument 1 of 'calc_celsius'| \asn44.c|23|note: expected 'float' but argument is of type 'float *'| \asn44.c|42|e

您的第一个程序在许多地方使用引用调用,但也有许多错误

1) 在调用printAll时使用CBR不正确。具体而言,这一行:

\asn44.c||In function 'printAll':|
\asn44.c|42|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|42|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|43|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|43|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|44|error: incompatible type for argument 1 of 'calc_celsius'|
\asn44.c|23|note: expected 'float' but argument is of type 'float *'|
\asn44.c|44|error: too few arguments to function 'calc_celsius'|
\asn44.c|23|note: declared here|
\asn44.c|49|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|49|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|50|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|50|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
\asn44.c|51|error: incompatible type for argument 1 of 'calc_fahr'|
\asn44.c|30|note: expected 'float' but argument is of type 'float *'|
\asn44.c|51|error: too few arguments to function 'calc_fahr'|
\asn44.c|30|note: declared here|
||=== Build failed: 12 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
对除最后一个参数外的所有参数使用CBR。这是错误的,因为&f1是指向float的指针,但printAll想要一个float(其他参数也是如此)

2) printAll只使用一个参数调用calc_celsius和calc_fahr,但这两个函数都需要两个参数

3) 您正在将浮点的地址传递给calc_celsius和calc_fahr,但您应该传递浮点本身

4) calc_fahr被声明为返回浮点值,但实际上不返回任何值

5) calc_celsius和calc_fahr都不需要第二个参数-只需返回表达式的结果(从第一个参数计算)


你的第二个程序也有很多错误,但基本上你只需要理解&f意味着指向f的指针。如果您的函数需要一个float,请传递一个,不要将指针传递给float。

您可以不再怀疑,将此程序转换为使用引用调用而不是值调用是否简单。这很简单,也很简单,只要你明白为什么要做这个转换

按值传递时,被调用函数参数是调用者传递参数的副本。更改副本,则原始参数不会更改

当您通过引用传递时,被调用的函数参数与调用者传递的参数是相同的变量,这意味着您传递的不仅仅是值,而是变量本身。如果被调用函数更改了参数,那么调用者的变量也会更改。你不再只是改变副本,而是真正的麦考伊

您的职能签名应为:

按值传递(传递一个值,另一个值由函数返回给调用方)

按引用传递(传递一个值,第二个值由函数修改)

当您通过引用传递时,您需要一个指针,指向发生更改的实际变量。这样,您就不需要向调用者返回任何值

两个版本的printall签名如下:

void calc_celsius(float, float*);
void calc_fahr(float, float*);
您实施的功能应该是:

通过值:

void printAll(float f1, float f2, float f3, float c1, float c2, float c3);
并通过参考:

float calc_celsius(float fahr) {
    return (fahr - 32.) * 5. / 9.;
}

float calc_fahr(float cels) {
    return (cels * 1.8 + 32.);
}
最后,对于两个程序都具有相同签名的printAll,对于pass-by-value版本应该是这样的:

void calc_celsius(float fahr, float* cels) {
    *cels = (fahr - 32.) * 5. / 9.;
}

void calc_fahr(float cels, float* fahr) {
    *fahr = (cels * 1.8 + 32.);
}
void printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

    printf("Fahrenheit \t | Celsius \n");
    printf("***************************** \n");
    printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
    ...
对于参考传递版本,如下所示:

void calc_celsius(float fahr, float* cels) {
    *cels = (fahr - 32.) * 5. / 9.;
}

void calc_fahr(float cels, float* fahr) {
    *fahr = (cels * 1.8 + 32.);
}
void printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

    printf("Fahrenheit \t | Celsius \n");
    printf("***************************** \n");
    printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
    ...
void printAll(float f1, float f2, float f3, float c1, float c2, float c3);
float calc_celsius(float fahr) {
    return (fahr - 32.) * 5. / 9.;
}

float calc_fahr(float cels) {
    return (cels * 1.8 + 32.);
}
void calc_celsius(float fahr, float* cels) {
    *cels = (fahr - 32.) * 5. / 9.;
}

void calc_fahr(float cels, float* fahr) {
    *fahr = (cels * 1.8 + 32.);
}
void printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

    printf("Fahrenheit \t | Celsius \n");
    printf("***************************** \n");
    printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
    ...
void printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

    float cels, fahr;

    printf("Fahrenheit \t | Celsius \n");
    printf("***************************** \n");
    calc_celsius(f1, &cels);
    printf("%.2f \t\t %.2f \n", f1, cels);
    calc_celsius(f2, &cels);
    printf("%.2f \t\t %.2f \n", f2, cels);
    ...