I am currently reading on the ways of passing arguments to C functions. And while reading I came to know that in C there are two methods to pass arguments as pass by value and pass by reference. Then again I read that we can pass a pointer to the variable as a parameter too.
The beginnersbook website this method is mentioned as pass by reference.
In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.
I know that we can pass a pointer variable or the address of the variable with & operator when we need to change or access the original variable. I want to know whether this pass by pointer can also be called pass by reference. Is pass by pointer also a method of pass by reference?
Technically, all arguments to functions in C are pass by value. The language doesn't have support for true references unlike C++.
Passing by reference can be emulated in C by passing (by value) the address of the variable you want to modify, then subsequently dereferencing that address to modify the pointed-to value. This is not true pass by reference, however.
Pointers in C are references. C 2018 6.2.5 20 says:
… A pointer type describes an object whose value provides a reference to an entity of the referenced type…
When you pass a pointer to a function, you are passing the function a reference to whatever object the pointer points to. The pointer itself is passed by value, but, because the pointer is a reference and you are passing it, the description that you are passing an object by reference is accurate.
Prior to the development of C++, there was no dispute about this; people said they were passing an object by reference to describe passing a pointer to the object. C++ adopted the word “reference” as a name for a new feature in its language, so, in C++ terminology, “reference” generally refers to that feature unless otherwise stated or made apparent by context. However, in C, we are not obliged by C++ terminology, and the original meaning of pass by reference remains accurate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With