-2

Specifics matter. Especially when talking about how something works, and even more so when we consider why something works. Currently, as I understand it, EVERYTHING in C is passed by value. NOTHING is passed by reference. Some programmers mention that arrays in C are passed by reference. But as per my limited understanding,

Even if we pass an array to a function like this void traverse(int arr[4]);, it is actually being taken in as a copy of the pointer variable storing the location in memory of the first element in that array. It is then dereferenced inside the function, but the initial value being passed is actually a local variable. Since memory allocated to arrays in the program stack would be contiguous, the compiler is able to make square bracket notation work as well as pointer arithmetic.

This and passing by reference are not the same thing to me. I would think this is an important distinction.

But on the other hand, we can then just say that everything in computing is passed by value, since something like Java would do the same in a more subtle manner. And it is actually just simulating a pass by reference. Please advise.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mindoverflow
  • 730
  • 4
  • 13
  • c does not have references, pointers are not references, pointers are passed by value (ie value of the pointer is passed) – Iłya Bursov Feb 10 '22 at 04:07
  • that is what i said. a copy of the pointer var is passed. that was not the question. – mindoverflow Feb 10 '22 at 04:09
  • plase correct me if i am wrong but we are able to extract the actual location of value stored in a specific location in memory by dereferencing the pointer. that is how we get the reference, right? – mindoverflow Feb 10 '22 at 04:11
  • 1
    no, pointer is the actual location of value, it is address of value, dereferencing pointer means getting actual value – Iłya Bursov Feb 10 '22 at 04:12
  • sorry my mistake. i meant that dereferencing a pointer would give us the actual value currently stored in the memory location represented by the pointer. – mindoverflow Feb 10 '22 at 04:13
  • and i was asking whether that value that we have gotten is the actual reference – mindoverflow Feb 10 '22 at 04:14
  • if this is correct, then my question is still valid. – mindoverflow Feb 10 '22 at 04:14
  • value is not reference, value is value – Iłya Bursov Feb 10 '22 at 04:14
  • yes, but dereferencing the pointer to get the value is in effect getting the reference of the value we intended to pass to some function, correct? – mindoverflow Feb 10 '22 at 04:16
  • no, getting value gives you value, not reference – Iłya Bursov Feb 10 '22 at 04:16
  • if passing by reference refers to passing the address of an argument, and a single pointer var the we have declared to be storing let's say value of address of U is passed to a function, and we have dereferenced said pointer INSIDE function, then we have technically extracted the reference INSIDE the function. therefore, it WAS passed by value, but the reference was gotten after it was passed. i am agreeing that everything in c is passed by value. please correct me if I am misunderstanding. – mindoverflow Feb 10 '22 at 04:21
  • passing by reference does not refer passing of the address, you're mixing references from c++/c# and word dereference from c, while they look similar - they are totally different concepts – Iłya Bursov Feb 10 '22 at 04:24
  • if we want to edit value of U in a function, we pass the function a proxy pointer var. in that way, we are getting the reference of U, which was intended, I don't mean the reference of the pointer var. – mindoverflow Feb 10 '22 at 04:25
  • by passing pointer we're getting pointer (not proxy, but just pointer), it is wrong to call pointer a reference in programming to avoid confusion – Iłya Bursov Feb 10 '22 at 04:28
  • No, essentially I am just saying that this is how I would try to explain to someone that would ask how we are editing the value even if we don't pass by reference – mindoverflow Feb 10 '22 at 04:28
  • the pointer is not the reference, by dereferencing the pointer we get the actual reference of the value. – mindoverflow Feb 10 '22 at 04:28
  • thus by using that reference, we are no longer changing the local var, but the actual var. that was my point – mindoverflow Feb 10 '22 at 04:29
  • i was just trying to ask what method i could describe to someone who seems to think that arrays are being passed by ref in c – mindoverflow Feb 10 '22 at 04:30
  • i would really appreciate it if you could provide some further reading on the matter. I have looked at https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value?rq=1 – mindoverflow Feb 10 '22 at 04:32
  • c does not have references, so "passing by reference" does not make any sense in context of c – Iłya Bursov Feb 10 '22 at 04:32
  • this is what the question is. i cannot simply claim something, but i have to be able to prove it. just saying because there is no pass by ref there is no pass by ref is not sufficient, correct? – mindoverflow Feb 10 '22 at 04:33
  • Just tell them that array values elide to a pointer value. It's much simpler than all the hair-splitting going on in this discourse, which to be honest is likely to confuse that person way more. It's fine to talk about "pass by reference" in terms of semantics. If a colleague started having this argument with me, I'd kick them up the backside. Unless we were in the business of actually writing compilers. – paddy Feb 10 '22 at 04:35
  • no i really need the discourse because i often think about this. i would really like to learn what is actually happening. a reference is just such that changing the ref would change the actual var globally as described in my previously linked SO link. we DO get a reference to our _desired_ var, but we do it _inside_ function, _after_ passing a pointer var and using that to dereference aka get the reference for the value _we actually intended_. i apologzie for the long discussion, but please bear with me. – mindoverflow Feb 10 '22 at 04:38
  • so there _are_ references in C, but not explicitly. is this accurate? – mindoverflow Feb 10 '22 at 04:40
  • the question was intended to reassure myself that everythign in C is indeed passed by value, and we are able to get the ref and thus able to change the desired var, but only after we have deref the pointer INSIDE the func. – mindoverflow Feb 10 '22 at 04:42
  • and so, even though we got the ref and were able to change our desired value, we did not PASS the ref to the func. we passed the VALUE of pointer which we used inside the func to then get the ref. hence all is pass by value in C. – mindoverflow Feb 10 '22 at 04:43
  • when pointer is dereferenced we're not getting new reference, we're either getting value referenced by pointer or we're changing the value referenced by pointer – Iłya Bursov Feb 10 '22 at 04:46
  • yes. there is no new reference. but we _are_ getting _some_ reference, right? otherwise, we would not be able to change the value. is this accurate? – mindoverflow Feb 10 '22 at 04:48
  • 2
    A reference is a concept. You can achieve reference-like behavior in C through the use of pointers. We frequently use the terminology "reference" and "dereference" to describe either adding or removing one level of indirection, but that is not to be confused with actual language features. In C++, actual references exist as a feature of the language. Yet we still often use the terms "reference" and "dereference" when describing pointer indirection. It is not the same thing as "having references". – paddy Feb 10 '22 at 04:48
  • `but we are getting some reference, right?` no – Iłya Bursov Feb 10 '22 at 04:48
  • @paddy yes! that was what i was trying to convey. we are acheiving reference-like behavior. emulating reference concept. so I am trying to say that we are _in effect_ getting the reference to desired value by de-reffing the pointer. – mindoverflow Feb 10 '22 at 04:50
  • @paddy if we leave out C++ refs, so far, are my explanations of how some reference is obtained _such that we can actually change the value CURRENTLY stored in that particular location sotred in the pointer var_ accurate? – mindoverflow Feb 10 '22 at 04:52
  • `in effect getting the reference to desired value by de-reffing the pointer.` no – Iłya Bursov Feb 10 '22 at 04:52
  • @IłyaBursov can you please explain what is actually happening when we de-ref. because if we don't have a ref (as defined by the highly accepted SO answer), we cannot change the var vaue outside of the function. – mindoverflow Feb 10 '22 at 04:53
  • getting ref to the desired variable i meant, sorry. – mindoverflow Feb 10 '22 at 04:54
  • we have address (memory location) where value is stored, dereferencing means that we access that memory location and as soon as this memory location is also used for some variable outside of the function - we can change it's value – Iłya Bursov Feb 10 '22 at 04:54
  • yes yes. that is the truth. essentially, i am getting somewhat of a temporary ref, for that value that is being CURRENTLY stored in the mem location stored in the pointer. if the mem location stores something else then we'd get a ref to that stored thing. we can think of it as having a direct phone line to anything stored in that specific mem loc. right? – mindoverflow Feb 10 '22 at 04:57
  • because we cannot change the value if we never have any reference to it in any way shape or form. – mindoverflow Feb 10 '22 at 04:57
  • it looks like some kind of trolling, I give up – Iłya Bursov Feb 10 '22 at 05:01
  • 1
    This is true. Java passes objects "by reference" and intrinsics "by value" - the way they are passed looks identical in the source code. You cannot tell by looking at the source code of a function call if a variable will be updated or not. Java does some magic underneath to make it work that way. But in C, you know what you pass can never be modified - everything is passed by value. So a pointer variable stores the address of a variable and the dereference operator (`*`) returns the item at that address but that item can be modified so you don't need pass "by reference" in C. – Jerry Jeremiah Feb 10 '22 at 05:03
  • WHY would I troll in SO? does this seem funny to you in any way? You simply make statements without backing them up. that is why the discussion keeps going on. i am providing reasoning for every single one of my claims, and you are providing very little. either correct me or present a counter argument. otherwise you may remove yourself from the discussion. all the best. – mindoverflow Feb 10 '22 at 05:03
  • @JerryJeremiah yes. in fact, I am trying to make a distinction especially because i originally started programming with java. so i am having difficulty with arbitrary usage of ref/ value passing. in C everything is passed by value. this is well documented. but then how are vars being changed? my explanation is they are being changed by getting the pointer var that was passed by value, and then de-reffed to get the 'reference' of the desired actual intended var. and that is how this change is registered even outside of the function. – mindoverflow Feb 10 '22 at 05:09
  • 1
    In C, if `int x=3`, 3 will be stored in memory (for example at address 12345). You pass `x` to a function. If you pass `x` the value 3 is copied from address 12345 onto the stack (for example to 678) The function parameter variable is stored at address 678. If the function changes the parameter it is updating address 678. The function has NO way to update address 12345. So instead, you `int* p = &x` and `p` now contains 12345. You pass it to the function and the function parameter variable `x` contains 12345. Dereferencing address 12345 (`*x`) produces 3 but you can assign as well: `*x = 5;` – Jerry Jeremiah Feb 10 '22 at 05:24
  • 1
    Maybe your question is more "How does Java simulate pass by reference? Does it sneakily use pointers under the hood the same way you have to manually do it with C?" – Jerry Jeremiah Feb 10 '22 at 05:25
  • Perhaps, but I know Java does indeed use pointers under the hood. I was simply trying to understand how exactly C is able to change values without passing by ref. I provided my current understanding of how I interpret C does it, and was wondering whether or not my concept was accurate. because that is how i explain it to people. Or perhaps during the time of C there was no such concept as "passing by reference"? and therefore i am having a hard time explaining it to other people who are more experienced and cannot see where i, as a novice, am coming from... – mindoverflow Feb 10 '22 at 05:30
  • because, if we follow the defn of reference from this link (https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value?rq=1), then such an entity must exist for any language to accomplish affecting a variable via a function. – mindoverflow Feb 10 '22 at 05:32
  • 1
    This might be useful: https://stackoverflow.com/questions/1856680/origin-of-term-reference-as-in-pass-by-reference – Jerry Jeremiah Feb 10 '22 at 05:34
  • It might help if you [edit] your question and explain what is the difference _for you_ between a pointer to an object and a reference of an object, if passed as an argument. -- Don't add new information here in the comments, they are already a burden to read, and that is not their purpose. This is not a forum. – the busybee Feb 10 '22 at 07:56
  • @IłyaBursov: Re “passing by reference does not refer passing of the address”: Passing by reference does refer to passing of the address. This was invented in assembly language, and implemented manually, long before C++ adopted the term for a different use. – Eric Postpischil Feb 10 '22 at 11:29
  • @mindoverflow: Re “the pointer is not the reference, by dereferencing the pointer we get the actual reference of the value”: The pointer is the reference; C 2018 6.2.5 20 says so. In C, when we dereference a pointer to an object, we get the object the pointer points to. That is, the result of dereferencing a pointer to an object produces an lvalue, a technical entity that designates the object. It is not the value of the object, but it is automatically converted to the object when used in an expression except as the left operand of an assignment or other exceptions listed in C 2018 6.3.2.1 2. – Eric Postpischil Feb 10 '22 at 11:32
  • @mindoverflow: Re “i was just trying to ask what method i could describe to someone who seems to think that arrays are being passed by ref in c”: When you pass an array in C, the actual bits put in the process register or memory location for the argument being passed are the bits for the address of the first element of the array. – Eric Postpischil Feb 10 '22 at 11:33

1 Answers1

3

At the level of bits in the computer, arguments can only be passed by value. Bits representing some argument are written to a processor register or memory location designated as the place to pass an argument. Passing by reference is a construct built upon passing by value by using an address as the value that is passed. Passing an address can be implemented automatically or manually. Both methods are pass-by-reference.

When we pass some entity by passing its address instead of passing its value directly, that is called pass by reference. This terminology long antedates the creation of “references” in C++. In assembly language, when we load the address of some thing into a register to pass it to a function, that was, and is, called pass by reference. The C standard specifies a pointer provides a reference to an entity (C 2018 6.2.5 20). So, when we have a pointer to an object, we have a reference to an object, and when we pass the pointer to a function, we are passing a reference to the object to the function.

Some languages automated pass-by-reference. FORTRAN passes everything by reference except for some special syntax for calling routines outside FORTRAN. However, whether passing-by-reference is implemented as an automatic feature of the programming language, by a programmer manually loading an address in assembly language, or by a programmer manually requesting an address with a language operator such as C’s &, when a reference to an object is passed, then the object is passed by reference.

C++ created a new type that it called a “reference,” but this was a new use of the word. The C++ meaning of “reference” applies to C++ only. It does not change the existing use of that word outside the context of C++. Outside of C++, “reference” has its ordinary English meaning of providing information on another thing.

Regarding your specific question about passing an array in C, in C, an array argument is automatically converted to the address of its first element, and this address is typically used to access the entire array. So the array is in fact passed by reference. Describing this as an automatic conversion to a pointer is merely documenting the details. The effect is the same: The function is given access to the object the caller designated by providing a reference to it.

Further, any dispute over the meaning of “pass by reference” is merely one about terminology, not about the actual mechanisms used in the computer.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thank you for clearing things up without criticizing my lack of knowledge. This is _extremely_ helpful to me. The person posting absolute statements without any reference actually destroyed some correct concepts I previously had. – mindoverflow Feb 11 '22 at 08:50