C FunctionsLesson 25Free preview
C Pass By Reference
This lesson is part of C · All levels
Summary
C does not have the built-in call by reference mechanism as some other languages do, but the same effect can be achieved by passing a pointer-shaped memory address to the function. The function then writes the value through a pointer with an operator*, so that the original variable in the caller goes along with the change. This pattern is often used to return more than one result at a time, for example a function that searches for the maximum value and its index. The advantage is that big data doesn't need to be copied, so it's more economical.
Key points
- Pass by reference in C is copied by passing the pointer to the function
- In a function, the original value is changed by dereference such as *a = 10.
- Caller sends address with a sign & on the argument
- It's suitable for big data because only addresses (not all data) are copied.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.