1) When you declare an array, you have to know the size to begin with. For instance,
Code:
const int size = 3;
double myArray[size] = {1.5, 2.5, 3.5};
cout<<"The size of the array is: "<<size<<endl;
If you want to send the array to a function and inside the function you need to know the size, then you should define the function to also accept the size of the array. For instance,
Code:
void myFunc(double anArray[], int size)
{
for(int i = 0; i<size; i++)
{
cout<<anArray[i]<<endl;
}
}
2) If you insist on using sizeof to get the size of the array, you can use an expression like this:
sizeof arrayName/sizeof arrayName[0]
For instance,
Code:
#include <iostream>
using namespace std;
class Apple
{
public:
int weight;
};
int main ()
{
const int size = 3;
Apple appleArray[size];
for(int i = 0; i < sizeof appleArray/sizeof appleArray[0]; i++)
{
appleArray[i].weight = i+1;
}
for(i= 0; i < sizeof appleArray/sizeof appleArray[0]; i++)
{
cout<<appleArray[i].weight<<endl;
}
return 0;
}