-
Arrary Size
I might just be too over accustom to vectors, but how do I get the size of an array in C++ again?
I had to do this
Code:
int num[] = {0, 0, 0, 0};
to fill up an arrary with only 4 zeros initially. Later on in the code I had to use a for loop to fill up the actual values, but can't remember how to get the size of the arrary so had to start from
Where 4 is the size of the array...
-
Code:
sz = sizeof(num)/sizeof(num[0]);
-
Just remember that that will only work when you do it in the same scope as you created the array in. For example this wont work as wanted:
Code:
#include <iostream>
using namespace std;
void func(int ar[])
{
cout << sizeof(ar)/sizeof(*ar) << endl;
}
int main()
{
int array[5];
cout << sizeof(array)/sizeof(*array) << endl;
func(array);
}
Will produce this output:
5
1