Jan 6, 2018 at 11:37am UTC
Why does ARRAY_SIZE work only in 'main' and not in 'printarray'?
#include <iostream>
#define ARRAY_SIZE(array) ((sizeof(array))/sizeof(array[0])) //array size
void printarray (int arg[])
{
int length = ARRAY_SIZE(arg); //why is ARRAY_SIZE(arg) only 1 ?
for (int n=0; n<length; ++n) //length==1 so only first element is printed
std::cout << arg[n] << ' ';
std::cout << '\n';
for(int elem : arg) //also, why does this give an error?
std::cout << elem << " ";
}
int main ()
{
int myAarray[] = {5, 10, 15};
std::cout << ARRAY_SIZE(myAarray); //in main, works perfectly
printarray (myAarray);
}
Jan 7, 2018 at 1:03am UTC
Last edited on Jan 7, 2018 at 1:03am UTC