1

Can someone help me figure out why this is not working.

int main() {
int A[100];

    for(int i=0; i<A.length; i++)
    {

    }

    return 0;
}

Here is my error

[Error] request for member 'length' in 'A', which is of non-class type 'int [100]'

GiBiT
  • 311
  • 1
  • 8
  • 20

2 Answers2

3

There is no such thing like length attribute for C style arrays. Consider using std::array and it's size() member, or sizeof(A)/sizeof(int), if you insists on C style arrays.

erenon
  • 18,838
  • 2
  • 61
  • 93
2

Plain arrays do not have any members. If you need to know their length, you either have to keep track of it, or you can write a simple function template to get it:

template<class T, size_t N>
constexpr size_t size(const T (&)[N]) { return N; }

then

int A[100];
....
for(int i=0; i < size(A); i++) { ... }

It would be far easier to use an std::array<int, 100>. This has a size method.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480