“ c&quot”中功能中的数据操作

发布于 2025-01-29 11:38:20 字数 866 浏览 3 评论 0原文

这是我不明白的事情。也许有人可以阐明它。

我知道数据操作的标准方法正在传递对函数的引用并更改函数中的数据。这样:

#include <stdio.h>
#include <stdlib.h>


void function2( float *param) {
    printf("I've received value %f\n", *param);
    (*param)++;
}

int main(void) {
    float variable = 111;

    function2(&variable);
    printf("variable %f\n", variable);
    return 0;
}

当使用(&amp; varible)调用功能2时,我希望该函数可以更改数据。 到目前为止,还没有问题。

但是为什么这也起作用呢?

#include <stdio.h>
#include <stdlib.h>


void function2( float &param) {
    printf("I've received value %f\n", param);
    (param)++;
}

int main(void) {
    float variable = 111;

    function2(variable);
    printf("variable %f\n", variable);
    return 0;
}

在我的理解中,当调用函数2(变量)时,“变量”值的副本将传递给函数。但是,在函数调用后,“变量”的值已更改。

在阅读这样的代码时,无论功能内部发生什么,我都不会期望“变量”的数据会更改。

Here's something I don't understand. Maybe someone can shed some light on it.

I know that the standard method for data manipulation is passing a reference to a function and changing the data in the function. Like this:

#include <stdio.h>
#include <stdlib.h>


void function2( float *param) {
    printf("I've received value %f\n", *param);
    (*param)++;
}

int main(void) {
    float variable = 111;

    function2(&variable);
    printf("variable %f\n", variable);
    return 0;
}

When calling function2 with (&variable) I expect that the function can change the data.
So far no questions.

But why does this work as well?

#include <stdio.h>
#include <stdlib.h>


void function2( float ¶m) {
    printf("I've received value %f\n", param);
    (param)++;
}

int main(void) {
    float variable = 111;

    function2(variable);
    printf("variable %f\n", variable);
    return 0;
}

In my understanding, when calling function2(variable) a copy of the value of "variable" is passed to the function. But nevertheless the value of "variable" has changed after the function call.

When reading a code like this I would never expect the data of "variable" to change, no matter what happens inside the function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晚风撩人 2025-02-05 11:38:20

该参数的声明

void function2( float ¶m) {

是不正确的。在C中没有参考。此类函数声明将在C ++中有效,其中存在引用。

当函数被调用时,就像

function2(variable);

创建了变量的副本一样。该函数是指主声明的原始变量。

This declaration of the parameter

void function2( float ¶m) {

is incorrect. In C there is no reference. Such a function declaration will be valid in C++, where references exist.

In C++ when the function is called like

function2(variable);

neither copy of the variable is created. The function refers to the original variable declared in main.

任谁 2025-02-05 11:38:20

变化1期望A 指针作为参数。因此,您需要明确传递&amp; variable

变体2期望a 参考作为参数。它们像一个指针一样 Kinda ,因为它们仍然指向内存中的原始位置,但也有些不同,因为它们键入明智,比单纯的记忆中的地址要多。就像,如果我没记错的话,您不能使用引用进行时髦的指针算术。

如您所见,如果一个函数期望参考作为参数,则编译器将知道该怎么做,而无需明确创建参考。

另外,如您所见,如果有人调用您的功能,并且没想到它会更改原始数据,这可能会带来令人讨厌的惊喜。这就是为什么在干净的代码中,您不会引入意外的副作用;如果功能与输入混乱,那应该是唯一的事情,并且应该非常明确。

要最终获得一个不做 更改原始的变体,请

void function2( float param)

在函数声明中进行。否*和否&amp;。在这种情况下,该函数不会获得指针或参考,而只是仅获得浮点值,现在将是输入参数的 copy ,因此不会更改您的变量。

Variation 1 expects a pointer as argument. Hence you need to explicitly pass in &variable.

Variation 2 expects a reference as argument. They're kinda like a pointer in that they still point to the original place in memory, but also a bit different in that they're, type wise, a bit more than a mere address in memory. Like, you can't do funky pointer arithmetic with a reference if I recall correctly.

As you can see, if a function expects a reference as argument, the compiler will know what to do without you having to explicitly create a reference.

Also, as you can see, this has the potential for nasty surprises if someone calls your function and didn't expect it to change the original data. That's why in clean code you don't introduce unexpected side effects; if a function messes with input that should be the only thing it does and it should be quite explicit.

To finally get a variation that does not change the original, do

void function2( float param)

in the function declaration. No * and no &. In this case the function doesn't get a pointer or a reference but simply a float value, which will now be a copy of the input argument and thus won't change your variable.

骑趴 2025-02-05 11:38:20

您发布的代码不会使用C编译器编译,因为它不是有效的C代码。 C中没有参考数据类型,不同于C ++。变量或参数名称不能以ampersand开头。如果要在C中写入。最受欢迎的是GNU C编译器(GCC) https ://gcc.gnu.org/ ,tinycc的替代品是最常见的分布存储库中。

The code you posted does not compile with a C compiler as it is not valid C code. There are no reference data types in C unlike C++. A variable or parameter name can not begin with an ampersand. Please use a C compiler if you want to write in C. The most popular one would be The GNU C Compiler (GCC) https://gcc.gnu.org/ and a ligher alternative would be the TinyCC, both available in most common distributions repositories.

嘿看小鸭子会跑 2025-02-05 11:38:20

您的第一个代码示例使用指针来(即)参数的地址。您将其称为“参考”,从理论上讲不是错误的。但是,由于在C ++中,参考是不同的(见下文),因此将此术语用于指针并不常见。

您的第二个代码示例确实使用了参考。但这不是C。参考是C ++功能。这意味着您使用的编译器接受此(并且可能是C ++编译器)。

C ++是C的超集,这就是为什么程序似乎是C但使用C ++功能的原因。

Your first code example uses a pointer to (i.e. an address of) the parameter. You call this a 'reference', which isn't wrong theoretically speaking. But because in C++ a reference is something different (see below), it's rather uncommon to use this term for a pointer.

Your second code example does use a reference. But this is not C. References are a C++ feature. This means that the compiler you use accepts this (and is probably a C++ compiler).

C++ is a superset of C, that's why a program can appear to be C but uses C++ features.

念﹏祤嫣 2025-02-05 11:38:20

您发布的代码不会使用C编译器编译,因为它不是有效的C代码。 C中没有参考数据类型,不同于C ++。变量或参数名称不能以ampersand开头。如果要在C中写入。请使用C编译器。

The code you posted does not compile with a C compiler as it is not valid C code. There are no reference data types in C unlike C++. A variable or parameter name can not begin with an ampersand. Please use a C compiler if you want to write in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文