pointer as second argument instead of returning pointer?

后端 未结 7 504
长情又很酷
长情又很酷 2021-01-18 05:33

I noticed that it is a common idiom in C to accept an un-malloced pointer as a second argument instead of returning a pointer. Example:

/*functi         


        
7条回答
  •  感动是毒
    2021-01-18 06:01

    1) As Samir pointed out the code is incorrect, pointer is passed by value, you need **

    2) The function is essentially a constructor, so it makes sense for it to both allocate the memory and init the data structure. Clean C code is almost always object oriented like that with constructors and destructors.

    3) Your function is void, but it should be returning int so that it can return errors. There will be at least 2, probably 3 possible error conditions: malloc can fail, type argument can be invalid and possibly value can be out of range.

提交回复
热议问题