Updating pointers in a function

前端 未结 5 1447
鱼传尺愫
鱼传尺愫 2020-12-11 03:52

I am passing a pointer a function that updates it. However when the function returns the pointer it returns to the value it had prior to the function call.

Here is m

5条回答
  •  借酒劲吻你
    2020-12-11 04:39

    C uses pass-by-value for function parameter passing. The value of pSrc is just a copy of the pSrc present in main(), not the same entity as the pSrc from main(). That is why, any changes made to pSrc inside the function func()won't be reflected in main() (caller function).

    However, all the changes to *pSrc will sustain.

    Solution: To change the pSrc of main() from func(), you need to pass a pointer to pSrc (pointer to a pointer) from main() and adjust the data types accordingly. Also, in that case, please note, the pSrc should be modifiable (present in read-write memory). As it is currently written, the pSrc in main() is not modifiable.

提交回复
热议问题