Question

Does C support pass by const reference like C++? If not, are there other ways to make pass-by-value more efficient? I don't think it makes sense to pass references to a function only because it's more efficient.

Was it helpful?

Solution

C does not support references or passing by reference. You should use pointers instead and pass by address. Pass-by-value is efficient for primitive types, but does a shallow copy for structs.

In C++ it makes a LOT of sense to pass objects by reference for efficiency. It can save a ton of copying and calling of constructors/destructors when copy constructors are defined. For large data objects (such as std::list) it is impractical to pass-by-value because the list would be copied when passed. Here you should definitely pass by reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top