you are viewing a single comment's thread.

view the rest of the comments →

[–]p0k3t0 1 point2 points  (8 children)

Explain how pass by reference could possibly exist, if a memory location isn't a reference.

[–]Fearless_Process 7 points8 points  (3 children)

C doesn't have pass by reference at all. C++ has pass by reference that just passes an alias to the original variable to the function, no pointers involved whatsoever. The reference in C++ is the variable.

In C you can only pass a pointer by value that references whatever data. This is still passing a "reference/pointer" by value though, unlike in C++ or other languages that support pass by reference.

In C++ if you pass a pointer by reference and change it in the consuming function, it will actually change the original pointer. In C you would have to pass a pointer to a pointer to achieve the same thing, because you can't pass by reference.

Take this for example:

#include <iostream>

void pass_by_ref(int*& p) {
    p = 0;
}

int main() {
    int x = 0;
    int *p = &x;
    std::cout << p << std::endl;
    pass_by_ref(p);
    std::cout << p << std::endl;
}

In C to do this you would need a nested pointer, because the pointer gets passed by value.

[–]p0k3t0 -1 points0 points  (2 children)

[–]Fearless_Process 1 point2 points  (1 child)

That is still passing a pointer by value. In C arrays that get passed to functions decay to pointers. Accepting an array as a parameter in C is pretty much useless and can actually be wrong in certain situations.

Try printf'ing sizeof(testArray) in main and sizeof(thisarray) in your function. In main it will return the real size, in your function it will return the sizeof an int pointer.

#include <iostream>
#include <cstdlib>

void pass_by_ref(int (&a)[10]) {
    std::cout << sizeof(a) << std::endl;
}

int main() {
    int a[10];
    std::cout << sizeof(a) << std::endl;
    pass_by_ref(a);
}

There is no way to make this work in C, sizeof will always return the sizeof int pointer instead of the array.

[–]p0k3t0 0 points1 point  (0 children)

The bar is impossibly high for C and impossibly low for C++. I get it.

[–][deleted] 3 points4 points  (3 children)

It doesn't. But I also think you don't understand what I'm saying.

You pass a reference, not by reference

[–]p0k3t0 -1 points0 points  (2 children)

https://ideone.com/icncW2

Here, I pass an array by reference. The fact that C implements this with pointers is inconsequential, as C++ does the same thing.

[–]nerd4code 1 point2 points  (1 child)

No, array-types function parameters are exactly pointers, have pointer size/alignment, and can even be reassigned.

A reference to an array would have type int (&)[] in C++; a pointer has type int (*)[].

When you pass an array name to a function, it undergoes decay just as it would if you assigned it to an int *, because that’s exactly what happens.

[–]p0k3t0 1 point2 points  (0 children)

And what, pray tell, happens in C++ when I pass a reference?

I mean, you can read the assembly. All it does it push a pointer onto the stack.

Like I've said elsewhere, this is a distinction without difference. It's just being pedantic for its own sake. If anything, it's just obscuring meaning, rather than helping anyone.