Wednesday, February 20, 2019

c - calloc typecasting to pointer to pointer




What does this statement mean?



//allocated memory for Device info

(*PppsCoreStructure)->psDeviceDetails=(sDeviceDetails **)calloc(CORE_DEVICEINFO_SIZE, sizeof(sDeviceDetails*));


I know that '(*PppsCoreStructure)->psDeviceDetails' is a pointer to pointer. But I am not being able to imagine how calloc can return pointer to pointer? I'm a beginner please help


Answer





  • (*PppsCoreStructure)->psDeviceDetails is declared with type sDeviceDetails **

  • The return value of calloc() is of type void*

  • (sDeviceDetails **)calloc(...) casts the return value of calloc to be of type sDeviceDetails **



In the C++ language this type cast is essential, although in C++ one would normally not be using calloc and would probably be using C++ casts.



In the C language the type cast is not needed because a void* pointer is assignment compatible with all other pointer types.







In a comment you state




But I want know how sDeviceDetails* is different from sDeviceDetails**




and I suspect that this is in fact your real question.



The answer is simple enough: T* is a pointer-to-T and T** is a pointer-to-pointer-to-T.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...