how to get float type array size

Dec 3, 2014 at 10:10am
When I try to get the size of float type array, my complier keep sending me:
sizeof on array function parameter will return size of "float" instead of 'float[]', what does it mean? and how can I get the array size of xValues[] and yValues[] without using the current method?

1
2
3
bool Polygon::setPoints(int numPoints, float xValues[], float yValues[]) {
   int xLength = sizeof(xValues) / sizeof(xValues[0]);
   int yLength = sizeof(yValues) / sizeof(yValues[0]);
Dec 3, 2014 at 11:37am
float xValues[] is another way to write float* xValues. When array is passed to function it decays to pointer losing all additional type information (like array size). So sizeof(xValues) returns size of pointer which is not what you want.

You can:
a) pass size along your array pointers.
b) make your function take concrete specific array type like float xValues[10]. Obviously it prevents you from passing arrays of other size and dynamically allocated arrays.
c) Drop arrays and use proper containers which at least know teir size. Vector for example.
Dec 3, 2014 at 6:53pm
what's an example of passing size along array pointer or using vector to get the size?
Dec 3, 2014 at 7:19pm
what's an example of passing size along array pointer
bool Polygon::setPoints(int numPoints, float xValues[], std::size_t xLength, float yValues[], std::size_t yLength)

using vector to get the size?
1
2
3
4
bool Polygon::setPoints(int numPoints, std::vector<float> xValues, std::vector<float> yValues) 
{
    auto xLength = xValues.size();
    //http://www.mochima.com/tutorials/vectors.html 
Dec 3, 2014 at 9:48pm
I got the idea. But if the signature of the method cannot be changed, how can I deal with this?
Dec 4, 2014 at 5:08am
What does numPoints stands for? Isn't it number of points/length of arrays?
Topic archived. No new replies allowed.