Hi!
I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work with[code=cpp]sizeof(array)/sizeof(array[0])[/code]Now, this does work in some situations, but not in others. Here's what I mean:[code=cpp]#include <iostream>
int length(int * array){return sizeof(array)/sizeof(array[0]);}
int main()
{
int array1[4] = {3,2,1,0};
std::cout << "Length of array1: " << sizeof(array1)/sizeof(array1[0]) << "\n"
<< "length(arr ay1) = " << length(array1)
<< endl;
}
[/code]The output is:
Now, I'm guessing, that my function length is just checking the size of the pointer to the first element of the array. My question is: Is there any method, to determine the size of an array, after this has been passed to a function? I'd hate to have to pass on that kind of information as an argument to every function, that needs the size of the array, manually.
Greetings,
Nepomuk
I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work with[code=cpp]sizeof(array)/sizeof(array[0])[/code]Now, this does work in some situations, but not in others. Here's what I mean:[code=cpp]#include <iostream>
int length(int * array){return sizeof(array)/sizeof(array[0]);}
int main()
{
int array1[4] = {3,2,1,0};
std::cout << "Length of array1: " << sizeof(array1)/sizeof(array1[0]) << "\n"
<< "length(arr ay1) = " << length(array1)
<< endl;
}
[/code]The output is:
Code:
Length of array1: 4 length(array1) = 1
Greetings,
Nepomuk
Comment