-1

So I thought that C++ works by value, except if you use pointers.

Although today I wrote this piece of code which works in a different manner than expected:

#include <iostream>

using namespace std;

void bubbleSort(int A[],int arrSize);

bool binarySearch(int B[],int key,int arrSize2);

int main(){
    int numberArr[10] = {7,5,3,9,12,34,24,55,99,77};
    bool found;
    int key;


    bubbleSort(numberArr,10);
    /**
    uncomment this piece of code
        for(int i=0; i<10; i++){
        cout<<numberArr[i]<<endl;       
    }

    **/


    cout<<"Give me the key: ";
    cin>>key;

    found=binarySearch(numberArr,key,10);

    cout<<found;
}

void bubbleSort(int A[],int arrSize){
    int temp;

        for(int i=0; i<arrSize-1; i++){
        for(int j=i+1; j<10; j++){
            if(A[i]>A[j]){
                temp=A[i];
                A[i]=A[j];
                A[j]=temp;
            }
        }
    }
}



bool binarySearch(int B[],int key,int arrSize2){
    int i=0;
    bool found=false;

    while(i<arrSize2 && !found){
    if(B[i]==key)
    found=true;
    i++;
    }


    return found;
}

When running this it seems that the values in numberArr are changing (sorted) in the main() function as well, just uncomment the commented block.

Any thoughts on why are the values of numberArr changing in main function as well?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Alex Lemesios
  • 522
  • 9
  • 22
  • You are actually using pointers. `int B[]` is equivalent to `int *B` as parameter. – Ulrich Eckhardt Dec 07 '19 at 11:20
  • 1. @UlrichEckhardt It's not equivalent. 2. You probably want to be using `std::array` –  Dec 07 '19 at 11:57
  • It's two different ways to write the exact same thing, @Sahsahae, that pretty much counts as equivalent. Proof: Declare two functions with the same. If it was different, it would be an overload, thus two different functions. If the compiler considers them the same, you get an error about the redefinition though. – Ulrich Eckhardt Dec 07 '19 at 12:29

2 Answers2

3

int[] as a type is essentially still a pointer due to array decaying. Therefore, you are passing an array "reference" (as in a value that will be used to "pass-by-reference", not as in an actual C++ reference like int&) and not a "value."

The "value" of int[] or int* itself is still "pass-by-value," it's just that the value is being used to access the "pointed-at" object memory by reference.

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
  • Pointer is a value like anything else. –  Dec 07 '19 at 11:56
  • I don't see what changed. Everything in C++ is, if we use outdated terminology, "pass-by-value" only. Ability to bind values as direct (and mutating) references instead of copying them into the function, doesn't change that fact either. You provide a value, and compiler does the rest for you. Providing an array, since it decays to a pointer, all that is copied, is the pointer. That's all there is to it. –  Dec 07 '19 at 12:06
  • That's why I put the term in "quotes," as the "idea" of pass-by-reference is "there," but in reality, everything is pass-by-value, as you said. Maybe it could be edited to be more clear on this... – CinchBlue Dec 07 '19 at 12:10
2

In C++ you can't pass arrays by value. You are always passing a pointer to the first element. That is why you can switch from foo(int a[]) to foo(int * a) without breaking the code.

Example: Some IDEs create main functions like this: int main(int argc, char * argv[]) others as int main(int argc, char ** argv).

You may take a look at Why can't we pass arrays to function by value? This explains it very well.

SchnJulian
  • 182
  • 1
  • 11