I have one array with size of 5 and passing this array to one method. I want to get size of the array inside method. If i get size of array inside method, i'm getting only "1",but not "5".
int v[5];
v[0]=1;
v[1]=2;
v[2]=3;
v[3]=4;
v[4]=5;
cout<<sizeof(v)/sizeof(&v[0])<<endl; // here i'm getting size as 5
CreateArray(v);
void CreateArray(int val[])
{
cout<<sizeof(val)/sizeof(&val[0])<<endl; // here i'm getting size as 1
}
#include <iostream>
void fill( int val[], unsigned nVals, int value )
{
for ( unsigned i=0; i<nVals; ++i )
val[i] = value ;
}
void print(constint array[], unsigned nElements)
{
for ( unsigned i=0; i<nElements; ++i )
std::cout << array[i] << ' ' ;
std::cout << '\n' ;
}
int main()
{
int v[5] = { 1, 2, 3, 4, 5 };
constunsigned v_size = sizeof(v) / sizeof(v[0]) ;
// Note that sizeof(v) / sizeof(&v[0]) only worked by coincidence, since
// none of the elements of v are of type pointer to int
print(v, v_size) ;
fill(v, v_size, 10) ;
print(v, v_size) ;
}
Hi, Thanks for reply. Actually i forgot to mention this point in question. I want to get without passing size as parameter as i have 8 array parameters in my method, so i can't pass another 8 parameter for size. This is algorithm, so i can't ask user to pass size of the array as well.
Could you plz guide me how to get array size inside method without passing size as parameter ?
I want to get without passing size as parameter as i have 8 array parameters in my method, so i can't pass another 8 parameter for size.
Maybe you should reconsider the design.
This is algorithm, so i can't ask user to pass size of the array as well.
Why not?
Could you plz guide me how to get array size inside method without passing size as parameter ?
You cannot get the size of an array in a function to which it is passed as a pointer. You could only deal with arrays of a certain size. You could use another data structure. But you cannot get the size of an array passed as you describe.