且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

**在指针中的意义 - C语言

更新时间:2023-02-18 15:45:22

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.

相关阅读

技术问答最新文章