How to get array size inside method

Hi, i'm new to c++. here is my scenario.

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
}

Is there any way to get size inside method ?


Thanks
Is there any way to get size inside method ?


Sure. Pass the size to the method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

void fill( int val[], unsigned nVals, int value )
{
    for ( unsigned i=0; i<nVals; ++i )
        val[i] = value ;
}

void print(const int 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 };
    const unsigned 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 ?

Thanks in advance
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.
Sometimes I pass the pointer of the array and pointer of the end of array
something like

1
2
3
4
5
6
7
int fill( const int * begin, const int * end, int data ){
      while( being != end ){
           *begin = data;
           ++ begin;
      }
}


Other times I just used vector

1
2
3
int fill( vector<int>& data, int val ){
      for( int i = 0; i < data.size(); ++ i ) data[i] = val;
}


I don't know,
Maybe read vector STL ?
They only keep pointer of array
yet can call vector::size() function
Last edited on
Other times I just used vector


This. If you're coding in C++ (rather than C), learn how to use vectors, and don't ever look back :)
You might find the discussion over in this other thread to be illuminating.

Start here: http://www.cplusplus.com/forum/general/82110/#msg441555

But make sure to read andy's comments and click on the FAQ link as well.
Hope this helps.
Topic archived. No new replies allowed.