MoNur (0)

I'm new to C++ and I need your help! whenever I try to use size in my code it says size is undeclared. how should I declare it?!

My head is about to explode!!!!

You are viewing a single comment. View All
mwilki7 (943)

I think you want to be using sizeof instead of size
Using sizeof on a local array will give you the size in bytes.
Divide by the byte size to get the number of elements.

#include <iostream>
#include <string>

using namespace std;

int main()
    int storSpace[] = { 232,241,324,216,221,298,334,212,235,324,264,141 };
    cout << "Size of array (bytes): " << sizeof(storSpace) << endl;
    // sizeof(int) because storSpace is an int array
    cout << "Size of array (elements): " << sizeof(storSpace) / sizeof(int) << endl;

    return 0;


Size of array (bytes): 48
Size of array (elements): 12

If you don't have to use arrays, I would suggest using vectors so you don't have to do any length tracking or math in size calculation.