How do i write this so i pass an array into into a function additional to main as a parameter.
in other words how do i pass the array into a function and return a the value. what the program does is remove duplicates from an array. Thanks!
int main ( )
{ const int SIZE = 10 ;
int integers [ SIZE ] ;
int index ;
cout << "Please enter " << SIZE << " integers, hitting return after each one:" << endl ;
for ( index = 0 ; index < SIZE ; ++index )
cin >> integers [ index ] ;
int count = 0 ;
for ( int i = 0 ; i < SIZE ; ++i )
{ bool seen = false ;
for ( int j = 0 ; ! seen && j < count ; ++j )
if ( integers [ j ] == integers [ i ] )
seen = true ;
if ( ! seen )
integers [ count++ ] = integers [ i ] ;
}
If we define a function which could operate on Array the problem is we cannot copy an array and when we use the name of an array it is automatically converted to a pointer to the first element.
And if we cannot copy an array we can not write a function with array type parameter simply it will convert to the pointer to the first element. So function dealing with array is manipulating pointers to elements in the array indirectly.
1 2 3 4
// all the definitions are same
void arr(int*) { /* ... */ }
void arr(int[]) { /* ... */ }
void arr(int[20]) { /* ... */ }
Despite appearances, a parameter that uses array syntax is treated as if we had written a pointer to the array element type.