W
W
Wasya UK2021-02-11 16:23:30
C++ / C#
Wasya UK, 2021-02-11 16:23:30

How to output a dynamic array in C++?

I'm getting this problem: C++ this range-based 'for' statement requires a suitable function and none was found .

What am I doing wrong?

void printArray(int *arr = {}) {
    for (const auto &e : arr) {
        cout << e << endl;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Pavlov, 2021-02-11
@Stalker31

It is possible, if the length of the array is known, to change as follows:

void printArray(int *arr ,size_t size) 
{//size-длина массива
    for (size_t el=0;el<size;el++) {
        cout << arr[el]<< endl;
    }
}

R
r_makis, 2021-02-16
@r_makis

"range-based for" applies to containers as it uses iterators

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question