再次调用函数时,变量不应重置?

发布于 2025-02-13 17:42:23 字数 561 浏览 1 评论 0原文

我希望在执行开始时将指针变量的值初始化为null。该变量用于函数,其值也可能在函数中更改。但是,我不希望该变量的值将null重置为。相反,其值应等于其先前函数调用的更新值。不能使用全局变量。

void function(struct node *variable) {
  // changes value of pointer variable to something else
  variable = something;
}

void another_function(void) {
  // have to be initialised before passing in as argument or else will raise error
  struct node *variable = NULL;

  function(variable);
}

// then calling this function multiple times in main
another_function();
another_function();

帮助您将不胜感激。

I want the value of a pointer variable to be initialized to NULL at the start of the execution. This variable is used in a function and its value may also get changed within the function. But I do not want the value of this variable to reset to NULL whenever a function call is made. Instead, its value should be equal to the updated value from its previous function call. Cannot use global variables.

void function(struct node *variable) {
  // changes value of pointer variable to something else
  variable = something;
}

void another_function(void) {
  // have to be initialised before passing in as argument or else will raise error
  struct node *variable = NULL;

  function(variable);
}

// then calling this function multiple times in main
another_function();
another_function();

help would be much appreciated.

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

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

发布评论

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

评论(2

娇柔作态 2025-02-20 17:42:23

首先,指针变量变量enose_function的本地。它不在此范围之外。

其次,当您将其传递给函数时,该功能的actibal参数也是该功能的本地。您立即将值分配给该本地变量,但这不会更改您传递的变量

Firstly, the pointer variable variable is local to another_function. It does not live outside of this scope.

Secondly, when you pass it to function, the variable parameter of that function is also local to that function. You immediately assign a value to that local variable, but that doesn't change the variable you passed in.

陌路终见情 2025-02-20 17:42:23

在声明初始指针变量时,您可以使用 static 关键字。
仅当执行期间首次遇到时,它才会初始化。

但是到目前为止,您的代码可能不会执行您打算做的事情。
指针 函数是本地的,并且不会对actible in other_function中的variable的值有影响。
您应该将指针指针(也称为双分球或嵌套指针)用于此效果。

这是如何使用双分球的一个示例:

struct node {
    int value;
};


void function(struct node** variable) {

    if(*variable == NULL) {
        *variable = (struct node*)malloc(sizeof(struct node));
        
        (*variable)->value = 1;
    }

    printf("Value of variable is %d\n", (*variable)->value);

    // Creating another struct node just for the demonstration
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->value = (*variable)->value + 1;

    *variable = newnode;
}

void another_function() {
    static struct node* variable = NULL;

    function(&variable);
}

基本上,您通过指针的地址(位于堆栈上,将始终具有内存地址),使您可以修改此指针的内容(在接收功能中的堆)。

当您在函数中取出变量时,您得到的不是一个值,而是另一个指针。

在这种情况下,括号也很重要。

嵌套的指针确实会令人困惑,我希望我的解释有些清楚。

要真正了解它的工作原理,您需要了解堆栈上的内容以及堆上的内容。但这也许是另一次解释。

当然,在此示例中没有内存管理,而我所写的内容包含来自所有struct node的内存泄漏,这些泄漏永远不会被交易。
您应该根据程序架构弄清楚如何做。

You can use the static keyword when declaring your initial pointer variable.
It will only be initialized when first encountered during the execution.

But as for now, your code will probably not do what you intend it to do.
The pointer variable in function is local and will have no effect on the value of variable in another_function.
You should use a pointer of pointer (also called a double-pointer or a nested pointer) for this effect.

Here is an example of how to use a double-pointer :

struct node {
    int value;
};


void function(struct node** variable) {

    if(*variable == NULL) {
        *variable = (struct node*)malloc(sizeof(struct node));
        
        (*variable)->value = 1;
    }

    printf("Value of variable is %d\n", (*variable)->value);

    // Creating another struct node just for the demonstration
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    newnode->value = (*variable)->value + 1;

    *variable = newnode;
}

void another_function() {
    static struct node* variable = NULL;

    function(&variable);
}

Basically, you pass the address of a pointer (located on the stack, which will always have a memory address) which let you modify the content of this pointer (a memory address on the heap) in the receiving function.

When you are dereferencing variable in function, what you get is not a value but another pointer.

Also parenthesis are important in this case.

Nested pointers can really be confusing, I hope that my explanation is somewhat clear.

To really understand how it works you need to understand what it's on the stack and what it's on the heap. But maybe this is an explanation for another time.

Of course, there is no memory management in this example and what I wrote contains memory leaks from all struct node that never are deallocated.
You should figure out how to do it depending on your program architecture.

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