I'm trying to access an element of an std::array given its pointer in C++. Here's some code that illustrates my problem:
#include <iostream>
#include <array>
void func(std::array<int, 4> *);
int main()
{
std::array<int, 4> arr = {1, 0, 5, 0};
func(&arr);
}
void func(std::array<int, 4> *elements)
{
for (int i = 0; i < 4; i = i + 1)
{
std::cout << *elements[i] << std::endl;
}
}
I would expect this to print every element of the std::array on a new line. However, it doesn't even get past compiling:
main.cpp: In function ‘void func(std::array<int, 4ul>*)’:
main.cpp:16:22: error: no match for ‘operator*’ (operand type is ‘std::array<int, 4ul>’)
std::cout << *elements[i] << std::endl;
What's going on here?
Thanks!