0

I've been learning c++ for a while. Recently I saw a format of passing function parameters. It looks like this

typedef struct LNode {
    int data;
    struct LNode* next;
} LNode, *LinkList;

bool insertData(LinkList &L, int i){
    cout <<  l;
    return false;
}

I know that LinkList is equivalent to LNode* which means a head pointer. But what does LinkList & aka LNode* & mean under the circumstances?

It seems to me that passing a LNode* aka LinkList would be enough.

ZHAN LU
  • 51
  • 5
  • *"I know that `LinkList` is equivalent to `LNode*`..."* No they're not equivalent. `LinkList` is an object while `LNode*` is a type. Instead the more correct way of saying this would be that the type of `LinkList` is `LNode*`. Similarly, `LinkList&` is not the same as `LNode*&`. – Jason Oct 20 '22 at 04:00
  • Then why we can pass `LinkList` as a type in a function declaration? – ZHAN LU Oct 20 '22 at 04:05
  • I see you're using `typedef` which i didn't notice previously. This mean, `LinkList` is a type because you're using `typedef`. If you remove the typedef you'll get error because then `LinkList` will not be a type. – Jason Oct 20 '22 at 04:06
  • emmm, I got this. But why does this function `insertData` add an address symbol `&` when passing the first parameter. I thought `LinkList` is just enough – ZHAN LU Oct 20 '22 at 04:08
  • It is called "pass by reference". Look up that term and you'll find many posts for that. See [Where should I prefer pass-by-reference or pass-by-value?](https://stackoverflow.com/questions/4986341/where-should-i-prefer-pass-by-reference-or-pass-by-value) – Jason Oct 20 '22 at 04:09
  • I would also recommend using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which re also available as PDFs for free. These things are explained in any beginner c++ book. You'll also find this usefule: [Address-of operator (&) vs reference operator(&)](https://stackoverflow.com/questions/35594378/address-of-operator-vs-reference-operator) – Jason Oct 20 '22 at 04:11
  • 1
    https://www.learncpp.com/cpp-tutorial/lvalue-references/: Note that this site introduces references before pointers. In my opinion as well, references are more important in C++ than pointers are and should be taught first. The code you are showing here is a weird mix of C and C++ that you would only write when interfacing C++ with C. `typedef struct` is not something that is normally used in C++ code. – user17732522 Oct 20 '22 at 04:11
  • 1
    Thanks every one, I got the point here. The question here is mainly because I mixed up some C and C-plus-plus concepts – ZHAN LU Oct 20 '22 at 04:17
  • 1
    @ZHANLU C++ has references(both lvalue references and rvalue references) while C does not. So this must have caused the confusion. – Jason Oct 20 '22 at 04:23
  • @JasonLiam Thank you so much. But there is the last question I want to ask. The reasons of passing a pointer by reference are 1. avoid the cost of value copy 2. we may change the pointer value in the function. Am I right? – ZHAN LU Oct 20 '22 at 04:29
  • @ZHANLU See [Reason to Pass a Pointer by Reference in C++?](https://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c) that answers your follow up question. Another is: [C++ passing pointer reference](https://stackoverflow.com/questions/27827761/c-passing-pointer-reference). Also note that for built in types like pointers, `int`, `double` etc you don't usually have to worry about the cost of copying. – Jason Oct 20 '22 at 04:32
  • 1
    @JasonLiam thxs – ZHAN LU Oct 20 '22 at 04:33
  • Nitpick: "`LNode*` which means a head pointer" Only in the sense that any node in a list is also the head of a sublist. – Caleth Oct 20 '22 at 09:25

0 Answers0