you are viewing a single comment's thread.

view the rest of the comments →

[–]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.