cstructpass-by-referenceterminologypass-by-pointer

Expression "passing by reference" for structs in C


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.


Solution

  • 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.