pointer of a pointer in linked list append

后端 未结 7 1825
Happy的楠姐
Happy的楠姐 2020-11-30 09:49

I normally program in python. To increase performance of my simulations, I am learning C. I have a problem to understand the use of a pointer of a pointer when implementing

相关标签:
7条回答
  • 2020-11-30 10:18

    In essence, as Jite & the others said is correct.

    Whenever you want to have a change applied to a data structure in C (change performed by another function), you need to pass a "reference" to this data structure for the change to persist beyond the change() function completes. This is what happens in Python too, you pass references to Objects unless you explicitly make a copy. In C you have to specify what you want to do precisely. To simplify it even more, it's either:

    type data_struct

    change(data_struct) => here's a copy of my data_struct, make your change but I do not care in the caller function about the change you apply

    or

    change(&data_struct) => here's the address (a "reference" to) of my data_struct, apply your change, the caller function will see this change after it is applied.

    Now, depending of what the original "type" is, you might have * or **. Nevertheless, keep in mind that there is a limit to how many "indirections" you can have, not sure weather it is system or compiler determined, if anyone has an answer to that I'm a taker. I never had more than 3 indirections.

    0 讨论(0)
提交回复
热议问题