If I have declared a double *positions[2]
array, is there a way of finding the actual size of the double *positions[2]
array?
Is there like a sizeof
function to find the size?
I have tried an example, and it does not return the size as 4
If I have declared a double *positions[2]
array, is there a way of finding the actual size of the double *positions[2]
array?
Is there like a sizeof
function to find the size?
I have tried an example, and it does not return the size as 4
Why not to use sizeof
?
Expression
sizeof( positions )
will return the size of the array in bytes that is equal do 2 * sizeof( double * )
Or you can use standard class std::extent
to determine the num ber of elements in an array. For example
std::cout << std::extent<decltype( positions )>::value << std::endl;
As for your edited post then the declaration of the array does not correspond to the values you showed. The array is defined as having pointers as its elements not double values.
If you want to define an array that will be initialized with values
0.1, 2.5
10.0, 56.7
12.4, 20.1
69.9, 69.9
72.1, 45.0
then you should write
double positions[5][2] =
{
{ 0.1, 2.5 },
{ 10.0, 56.7 },
{ 12.4, 20.1 },
{ 69.9, 69.9 },
{ 72.1, 45.0 }
};
In this case sizeof( positions )
will be equal to 5 * 2 * sizeof( double )
Or
sizeof( position ) / sizeof( *positions )
will be equal to 5 and
sizeof( *position ) / sizeof( **positions )
will be equal to 2