在C中,在函数的参数中double Asterisk **做什么

发布于 2025-02-10 06:08:15 字数 319 浏览 2 评论 0原文

我在链接列表中遇到了一个代码片段,但这是我第一次看到参数中使用的“ **”双星号。它做什么?

// Adding an item to the beginning of the list
void push(node_t ** head, int val) {
    node_t * new_node;
    new_node = (node_t *) malloc(sizeof(node_t));

    new_node->val = val;
    new_node->next = *head;
    *head = new_node;
}

I came across a code snippet in linked list but this is the first time I see "**" double asterisk used in parameters. What does it do?

// Adding an item to the beginning of the list
void push(node_t ** head, int val) {
    node_t * new_node;
    new_node = (node_t *) malloc(sizeof(node_t));

    new_node->val = val;
    new_node->next = *head;
    *head = new_node;
}

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

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

发布评论

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

评论(3

黎歌 2025-02-17 06:08:15

如果您具有类似的变量

int x = 10;

,并且想在函数中更改它,则需要通过参考将其传递给该函数。

在C传递的C中,意味着间接通过指针向其传递变量。因此,将指针在功能中的指针中取消您可以直接访问原始变量并可以更改它。

这是一个演示程序。

#include <stdio.h>

void f( int *px )
{
    *px = 20;
}

int main( void )
{
    int x = 10;

    printf( "Before calling f x = %d\n", x );

    f( &x );

    printf( "After  calling f x = %d\n", x );
}

程序输出是

Before calling f x = 10
After  calling f x = 20

如果变量具有类型类型,例如

node_t *head = NULL;

,如果您要在函数中更改其值,则需要通过引用将其传递给该函数,

push( &head, 10 );

以便将相应的函数参数像

void push(node_t ** head, int val);

和将指针指向原始指针head,就像

*head = new_node;

您可以更改Main中定义的原始指针 head 。

如果您不会通过参考将指针传递,而是按值传递,则该函数将处理原始指针值的副本。更改副本不会影响原始指针。

传递给函数

#include <stdio.h>

void f( int x )
{
    x = 20;
}

int main( void )
{
    int x = 10;

    printf( "Before calling f x = %d\n", x );

    f( x );

    printf( "After  calling f x = %d\n", x );
}

考虑前面的演示程序,在这种情况下,原始变量通过值将原始变量x 。它是其价值的副本,它在功能中已更改。因此程序输出将是

Before calling f x = 10
After  calling f x = 10

If you have a variable like for example

int x = 10;

and want to change it in a function you need to pass it to the function by reference.

In C passing by reference means passing a variable indirectly through a pointer to it. Thus dereferencing the pointer within the function you have a direct access to the original variable and can change it.

Here is a demonstration program.

#include <stdio.h>

void f( int *px )
{
    *px = 20;
}

int main( void )
{
    int x = 10;

    printf( "Before calling f x = %d\n", x );

    f( &x );

    printf( "After  calling f x = %d\n", x );
}

The program output is

Before calling f x = 10
After  calling f x = 20

If a variable has a pointer type like for example

node_t *head = NULL;

then again if you are going to change its value in a function you need to pass it to the function by reference as for example

push( &head, 10 );

So the corresponding function parameter will be declared like

void push(node_t ** head, int val);

And dereferencing the pointer to the original pointer head like

*head = new_node;

you can change the original pointer head defined in main.

If you will not pass the pointer by reference but pass it by value then the function will deal with a copy of the value of the original pointer. Changing the copy does not influence on the original pointer.

Consider the preceding demonstration program where the original variable is passed to the function by value

#include <stdio.h>

void f( int x )
{
    x = 20;
}

int main( void )
{
    int x = 10;

    printf( "Before calling f x = %d\n", x );

    f( x );

    printf( "After  calling f x = %d\n", x );
}

In this case the original variable x will not be changed. It is a copy of its value that was changed within the function. So the program output will be

Before calling f x = 10
After  calling f x = 10
吖咩 2025-02-17 06:08:15

在这种情况下,它声明指向node_t指针的指针,这是函数参数。

这是一个使用字符指针的简单示例:

char *charPointer = "This is a text.";
char **pointerToCharPointer = &charPointer;

printf("%s\n", charPointer);
(*pointerToCharPointer) = "This is something new.";
printf("%s\n", charPointer);

输出:

This is a text.
This is something new.

In this case, it declares a pointer to a node_t pointer, which is a function parameter.

Here is a simple example using character pointers:

char *charPointer = "This is a text.";
char **pointerToCharPointer = &charPointer;

printf("%s\n", charPointer);
(*pointerToCharPointer) = "This is something new.";
printf("%s\n", charPointer);

Output:

This is a text.
This is something new.
原谅过去的我 2025-02-17 06:08:15

在这种情况下,这是因为该功能正在更新指针值。

请记住,要更新参数,我们必须将指针传递给该参数:

void update( T *ptr ) // for any non-array object type T
{
  *ptr = new_T_value(); // writes a new value to the
}                       // thing ptr points to

int main( void )
{
  T var;
  update( &var );  // writes a new value to var
}

现在,让我们用指针类型p *p * p *:

void update( P **ptr )
{
  *ptr = new_Pstar_value();
}

int main( void )
{
  P *var;
  update( &var );
}

该行为正是同样 - 我们通过表达式*ptr更新var的值,所有更改的都是类型。

In this case, it’s because the function is updating the pointer value.

Remember that for a function to update a parameter, we must pass a pointer to that parameter:

void update( T *ptr ) // for any non-array object type T
{
  *ptr = new_T_value(); // writes a new value to the
}                       // thing ptr points to

int main( void )
{
  T var;
  update( &var );  // writes a new value to var
}

Now, let’s replace T with the pointer type P *:

void update( P **ptr )
{
  *ptr = new_Pstar_value();
}

int main( void )
{
  P *var;
  update( &var );
}

The behavior is exactly the same - we’re updating the value of var through the expression *ptr, all that’s changed are the types.

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