Thread: Need explanation on function data pass

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    5

    Need explanation on function data pass

    Hallo
    I start to learn C++
    And now I am in the lesson of passing array in a function as argument.
    I played with that and I made that code:

    Code:
    #include <iostream>
    using namespace std;
    
    void exa(int arr[]) {
    	
        for(int x = 0; x<5; x++){
           cout <<  arr[x] << endl;       
    // show the data that passed from the array
    // in the main function
        }
       cout  << "#########"<< endl;
       
       for(int a = 0; a<5; a++){
       	cin >> arr[a];
           cout <<  arr[a] << endl; 
    //Changing the data in that array (arr[]) 
    //to the input : 1, 2, 3, 4, 5
    // and then show the data
    }
    }
    
    int main() {
      
      int arry[] = {22, 43, 3, 12, 4};
       
       exa(arry);
       // passing the arry[] data to the function
       
       cout << "***********" << endl;
       
       for (int b =0; b<5;b++){
       	cout << arry[b] << endl;
    // checking the array data to see if it
    // remains the same
    
       }
      
    }
    So I dont understand why the data in the array in the main function (arry[]) change..
    Its should show me its original data
    (22, 43, 3, 12, 4) but instead it show me the data from the input(1,2,3,4,5)
    Why? What I am missing here..

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    Arrays are always passed as a pointer, so the array you see in the function IS the array in main.

    When you pass an array to a function like this.
    > exa(arry);
    What you've really written is this
    > exa( &arry[0] );



    Code:
    void foo ( int a ) { // by value
       a = 42;  // does NOT change in main
    }
    void bar ( int *a ) { // by pointer
      *a = 42; // does change in main
    }
    void baz ( int &a ) { // by reference
      a = 42;  // does change in main
    }
    int main ( ) {
      int v1 = 0, v2 = 0, v3 = 0;
      foo(v1);
      bar(&v2);
      baz(v3);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    5
    Quote Originally Posted by Salem View Post
    Arrays are always passed as a pointer, so the array you see in the function IS the array in main.

    When you pass an array to a function like this.
    > exa(arry);
    What you've really written is this
    > exa( &arry[0] );



    Code:
    void foo ( int a ) { // by value
       a = 42;  // does NOT change in main
    }
    void bar ( int *a ) { // by pointer
      *a = 42; // does change in main
    }
    void baz ( int &a ) { // by reference
      a = 42;  // does change in main
    }
    int main ( ) {
      int v1 = 0, v2 = 0, v3 = 0;
      foo(v1);
      bar(&v2);
      baz(v3);
    }
    Oh I understand now, didn't know that.
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass data using Sockets
    By chaituchemico in forum Linux Programming
    Replies: 17
    Last Post: 09-16-2011, 01:49 PM
  2. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  3. DLL's to pass data
    By MotorCityMadMan in forum Windows Programming
    Replies: 3
    Last Post: 05-10-2007, 11:36 PM
  4. pass extra data to a function
    By asmileguo in forum C++ Programming
    Replies: 12
    Last Post: 08-09-2006, 11:17 AM
  5. How to pass data between 2 Threads
    By dv007 in forum C Programming
    Replies: 2
    Last Post: 02-22-2006, 01:18 PM

Tags for this Thread