I have recently had a class about structures in C and the lecturer used the expression "pass by reference" to describe the action of passing a pointer towards a structure to a function. I have always read that C does not have "pass by reference" because, unlike Cpp, pass by reference can only be emulated by passing by value a pointer towards some object — and we should rather say "pass by pointer". So I am wondering whether or not things work differently for structures and this expression is justified in this context.
-
5The pointer to the object is "passed by value" so you get a "reference" to the object, traditionally called "pass by reference". – Paul Ogilvie Mar 08 '21 at 14:04
-
1They probably used the term colloquially. Of course, pass by reference _technically_ isn't a concept in C, but pass by pointer is basically an emulation of pass reference, in a way. You pass the address of the variable which the pointer parameter gets, hence it is essentially a "reference". – mediocrevegetable1 Mar 08 '21 at 14:06
-
Pass by reference is not a specific term for C++ specifically anyway. It simply means that when you pass an argument, you actually pass the argument itself instead of a copy. I'm sure there are languages apart from C++ which also pass by reference (pretty sure Pascal allows for pass by value and reference too), and perhaps even some which allow pass by reference _only_. – mediocrevegetable1 Mar 08 '21 at 14:11
-
Does this answer your question? [Passing by reference in C](https://stackoverflow.com/questions/2229498/passing-by-reference-in-c) – Mark Benningfield Mar 08 '21 at 14:15
4 Answers
So I am wondering whether or not things work differently for structures and this expression is justified in this context.
If you describe passing an int *
as "pass by pointer" then you should also describe passing a struct foo *
as "pass by pointer". There is nothing special about structures that would require or even suggest different word choice than for other kinds of objects.
In practice, there is a diversity of actual word usage in this area, but I am among those who argue against using the term "pass by reference" to describe passing pointers. I think there are good reasons for that, but they would be tangential.

- 160,171
- 8
- 81
- 157
The pass by value and pass by reference are the common wordings when we speak of passing parameters to subroutines or functions in a language agnostic way.
The former says that the caller will never see changes to the variables and is used for input only data, the latter says that the caller will see the changes and is used for input/output or output only data.
In C language, as the language only allow by value, the by reference way is emulated by using pointers, but it is an implementation detail and the goal is indeed to have pass by reference semantics.
For your precise question, nothing is special when it comes to structures, and it you want by reference semantics, you will have to pass a pointer.
The only special thing for structure, is that you can return a structure from a function, what was not allowed in the first K&R C versions in the 70's or early 80's.

- 143,923
- 11
- 122
- 252
Per the C standard, a pointer “provides a reference” (C 2018 6.2.5 20). This is a common English meaning for “reference”; if a newspaper article says “Judge Matthews ordered that…,” that is a reference to Judge Matthews.
When an object is passed by giving the called function a pointer to the object rather than by giving the value of the object, that is a call by reference.
C++ created a new feature and called it a “reference,” giving a new meaning to that term. The term has that meaning in the context of C++. It does not have that meaning in medicine, common English, archeology, C, or other contexts. In C, when we say something is passed by reference, we mean the value of a pointer to it is passed.

- 195,579
- 13
- 168
- 312
I have always read that C does not have "pass by reference" because, unlike Cpp, pass by reference can only be emulated by passing by value a pointer towards some object — and we should rather say "pass by pointer".
You're conflating two similar but different meanings of reference
. The phrase "pass by reference" doesn't necessarily pertain to C++'s reference types, which under the hood are essentially pointers with safer, more limited semantics. It means that instead of providing a value directly, you provide it by saying where it is. If I want you to paint a house, I can either take you to the house and tell you to paint it, or I can give you the address of the house; the address is an object that refers to another object, i.e. a reference.
Passing a pointer to an object to a function so that the original object may be modified has long been known as passing by reference in languages like C and Pascal, and that's likely why C++ reference types are so named. It's a general concept that applies to most languages. Passing by pointer seems to be a phrase that came into use after C++ introduced references as distinct types. In short, using pass by reference can be ambiguous in a mixed C/C++ context, so using pass by pointer is more specific when that's what you mean. But you should understand pass by reference in a language-agnostic context to mean any sort of reference, whether it's a specific type provided by the language or a more general pointer. And when talking about C specifically, pass by pointer and pass by reference mean the same thing.

- 124,013
- 19
- 183
- 272