简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM