**在指针中的意义 - C语言 [英] Significance of ** in pointers - C language

查看:204
本文介绍了**在指针中的意义 - C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C语言中对链表进行一些操作。我是一个新手,在函数 append()中变得困惑。



他们已经在函数中传递了参数,如 struct node ** q 。他们正在使用它与 * q



功能 append()在C语言中:

  void append(struct node ** q,int num){
struct node * temp,* r;
if(* q == NULL){
temp = malloc(sizeof(struct node));
temp-> data = num;
temp-> link = NULL;
* q = temp;
}
else {
temp = * q;
while(temp-> link!= NULL)
temp = temp-> link;
r = malloc(sizeof(struct node));
r-> data = num;
r-> link = NULL;
temp-> link = r;
}
}

我无法理解: -


  1. 为什么他们在参数和 * q中使用 ** q 在代码部分?

  2. 有什么区别?和

  3. 最好的方法是什么?

任何帮助将不胜感激。

解决方案>

记住,C中的参数通过值传递,这意味着它们的值被复制。所以要更改函数中的一个参数,你必须通过引用传递它。在C中,这是通过使用指针来完成的。然而,传递给函数的指针本身就是通过值传递的,所以为了能够改变那个指针,你必须通过引用传递它,所以你传递它作为指针的指针。






对于您的具体代码,函数追加修改了您传递给它的指针,因此您需要通过引用通过指针的地址传递它。调用者执行以下操作:

  struct node * queue; 
append(& queue,...);

然后当追加返回时, q 可能会更改。






使用 * q 在函数中是因为一元 * 运算符用于取消引用指针。所以如果你有一个指针 q 然后 * q 将是 q 指向。在你的功能的情况下,由于 q 是一个指向指针的指针,那么 * q 将导致原始指针



如果使用上面的短片段调用,那么 * q 将返回排队指针。


I am trying some operations on linked lists in C language. I am a newbie and sort of getting confused in the function append().

They have passed arguments in the function like struct node **q. And they are using it with *q.

Function append() in C language:

void append(struct node **q, int num) {
    struct node *temp, *r;
    if(*q==NULL) {
        temp=malloc(sizeof(struct node));
        temp->data=num;
        temp->link=NULL;
        *q=temp;
    }
    else {
        temp=*q;
        while(temp->link!=NULL)
            temp=temp->link;
        r=malloc(sizeof(struct node));
        r->data=num;
        r->link=NULL;
        temp->link=r;
    }
}

I am not able to understand:-

  1. Why they are using **q in arguments and *q in the code part?
  2. What is the difference ? and
  3. what is the best way to do it?

Any help would be appreciated.

解决方案

Remember that arguments in C are passed by value, meaning that their values are copied. So to change an argument in a function, you have to pass it by reference. In C this is done by using pointers. However, a pointer itself when passed to a function is also passed by value, so to be able to change that pointer you have to pass it by reference, hence you pass it as a pointer to the pointer.


For your specific code, the function append modified the pointer you pass top it, and so you need to pass it by reference with the address of the pointer. The caller does something like:

struct node *queue;
append(&queue, ...);

Then when append returns, q may be changed.


The usage of *q in the function is because the unary * operator is for dereferencing a pointer. So if you have a pointer q then *q will be the value that q points to. In the case of your function, since q is a pointer to a pointer then *q will result in the original pointer.

If called using my short snippet above, then *q will be returning the queue pointer.

这篇关于**在指针中的意义 - C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆