0

I need to print an array by implementing the use of a function before the main function. So I tried the following function:

int* printArr(int* arr)
{
    for (int i = 0; i < 5; i++) {
        cout << arr[i];
    }   
    return arr;
}

I encountered two problems when implementing this into the whole code.

First, this is printing what I think is the address of the array and not the actual array. Pretty sure this is because of the return arr; in line 10. But if I do not write return then the code will produce an error. How can I fix this?

Second, I do not understand the second argument printArr has. In line 19, you can see cout << printArr(arr, 5) << endl;. How come there is a single numeric value, that one being 5 in this case, as an argument? How can I account for this in my function?

Please keep in mind that I am new to C++ and coding in general. Also, this code was given to me, hence why I do not understand certain aspects of it.

This is my code so you can see what I mean:

#include <iostream>
using namespace std;

// Declare function printArr here

int* printArr(int* arr)
{
    for (int i = 0; i < 5; i++)
{cout << arr[i];}
    
    return arr;
}

int main()
{
    int arr[5] = {1, 3, 5, 7,9};
    int last_num = arr[sizeof(arr)/sizeof(int)-1];
    
    cout << "Before reversing" << endl;
    cout << printArr(arr, 5) << endl;
    
    // reverse "arr" using reference(&)
    
    cout << "After reversing" << endl;
    printArr(arr, 5);
    
    return 0;
}
9
  • What C++ compiler are you using? This should not compile. And the code you show is a mash of C and C++... is this for school? Commented Sep 22, 2020 at 18:32
  • 2
    printArr(arr, 5) should not compile. Commented Sep 22, 2020 at 18:33
  • 1
    cout << printArr is probably not desirable here. printArr returns a pointer, so this looks like it would just print the address of arr after everything else printArr would print. Commented Sep 22, 2020 at 18:35
  • In C++ you should be using std::vector as your go-to array container. Are you allowed to use that? If not, sigh, yet another "C++" course in name only. Commented Sep 22, 2020 at 18:42
  • Your printArr should take another parameter, int n as a number of elements in the array. Also, I don't see why it should return anything at all. may be make it void? Commented Sep 22, 2020 at 18:43

1 Answer 1

2

The declaration and implementation of printArr() is all wrong.

First, this is printing what I think is the address of the array and not the actual array.

The printArr() function itself is printing the contents of the array (well, the first 5 elements anyway), and then returning the address of the array. It is main() that is printing that address afterwards, when it passes the return value of printArr() to std::cout <<.

Pretty sure this is because of the return arr; in line 10. But if I do not write return then the code will produce an error. How can I fix this?

By getting rid of the return type altogether. There is no good reason to return the array pointer at all in this example, let alone to pass that pointer to std::cout. So printArr() should be returning void, ie nothing.

Second, I do not understand the second argument printArr has. In line 19, you can see cout << printArr(arr, 5) << endl;. How come there is a single numeric value, that one being 5 in this case, as an argument?

Because main() is passing in the element count of the array (5) so that printArr() can know how many elements to actually print, instead of hard-coding that value in the loop. However, your declaration of printArr() does not have a 2nd parameter with which to accept that value, that is why you are getting errors.

How can I account for this in my function?

By adding a 2nd parameter in the function declaration, eg:

#include <iostream>
using namespace std;

// Declare function printArr here

void printArr(int* arr, int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
}

int main()
{
    int arr[5] = {1, 3, 5, 7, 9};
    const int count = sizeof(arr)/sizeof(arr[0]);

    int last_num = arr[count-1];
    
    cout << "Before reversing" << endl;
    printArr(arr, count);
    
    // reverse "arr" using reference(&)
    
    cout << "After reversing" << endl;
    printArr(arr, count);
    
    return 0;
}

Live Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the explanations. I think I now understand what I was doing wrong. I still have a question about that same code. Can I have two functions with the same name printArr? Since you helped me resolve the issue with printing the array using the printArr function, now I have to reverse it. I was planning on reversing it using another function also named printArr.
@Rivf "Can I have two functions with the same name printArr?" - yes, if they have different parameters. It is called function overloading. "I was planning on reversing it using another function also named printArr" - that is not a good idea. Name your functions based on what they actually do. For instance, reverseArr would be more appropriate than printArr for a function that reverses an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.