Thread: array problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    192.168.1.1
    Posts
    4

    array problem

    this program uses read() function to input values and print out arrays with print() function
    however, i cannot get the arrays printed correctly...

    Code:
    void read(int[], int);
    void print(int[], int);
    int main()
    { const int MAXSIZE=100;
    int a[MAXSIZE]={0}, size;
    read(a,size);
    cout<<"The array has "<<size<<" elements: ";
    print(a,size);
    }
    
    void read(int a[], int n)
    {cout<< "Enter integers. Terminate with 0: \n";
    n=0;
    const int MAXSIZE=100;
    do
    {cout<< "a["<< n << "] : ";
    cin>>a[n];
    }while (a[n++] != 0 && n<MAXSIZE);
    --n;
    }
    void print(int a[], int n)
    {for (int i=0; i<n; i++)
    cout<< a[i]<< " ";
    }
    it always comes out as 0 array and no values being inserted.
    Thanks for the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The 2nd parameter of read() needs to be a reference parameter, for changes in read() to be visible to the size variable declared in main()

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    192.168.1.1
    Posts
    4
    can some one explain why reference parameter is needed in this program?
    thnx

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unless you say otherwise, size (in your code) will be passed by value.
    This means your function gets a copy of size, not size itself.
    Your function then modifies this copy (not the original), so by the time you get back to main, size is left as it was (uninitialised).

    You could also write the code like this
    size = read ( a );
    Where read is coded to return an int, and you finish the function with
    return n;

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Note that the reason your "a" parameter seems to be modified without passing it by reference is that it is a pointer variable. This means that the function is making a copy of the pointer contents you pass for "a" and, since the contents of said copy happen to be the address of in memory at which the array data begins, you are, in fact, able to modify the array data without modifying the pointer variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM