error : invalid conversion from 'int' to 'int*'

Viewed 47

I'm getting the following error in my code:

error : invalid conversion from 'int' to 'int*'

I have made a function printArray(), and when in my main body I am just using printArray() to come up with output. It works perfectly fine.

But when I write it like:

cout << "The array after swapping values will be " << printArray(arr, 6) << endl;

It shows that error.

#include<iostream>
using namespace std;

void reverse(int arr[] , int n)
{
    int start =0 ;
    int end= n-1;

    while ( start<=end){
        swap( arr[start] , arr[end] );
        start++ ;
        end-- ;
    }
}

void printArray(int arr[] , int n){
    for ( int i=0 ; i<n ; i++ ){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}


int main(){
    int arr[6]= { 1 , 2 , 3 , 4 , 5 , 6 };
    reverse(arr, 6);
    cout<< "The array after swapping values will be " <<printArray(arr, 6)<<endl;
    //printArray(arr, 6);

    return 0;
}

I just commented out the printArray(arr,6) line to get my output using cout, but it is showing the error.

It's my first ever question asked to StackOverflow, so I might not be able to explain it perfectly to you, and I am sorry for that. Please do help me with it.

1 Answers

The error message is confusing.

Nevertheless the function printArray has the return type void That is the function returns nothing.

void printArray(int arr[] , int n){

So this statement

cout<< "The array after swapping values will be " <<printArray(arr, 6)<<endl;

is incorrect.

You could split it like

cout<< "The array after swapping values will be ";
printArray(arr, 6);
cout<<endl;
Related