Milad
Milad

Reputation: 5500

Pass by const reference in C

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.

Upvotes: 5

Views: 5403

Answers (1)

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6039

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.

Upvotes: 9

Related Questions