//arr is the array
int length;//for recive the array size
length=sizeof(arr);//recive the array size
cout <<length;
length=length/sizeof(typeid(arr));//calculate the number of elements
cout << sizeof(typeid(arr));

i'm using these code for calculate the number of elements in array.
sizeof(typeid(arr)) these line is for give the type size, but isn't correct
can anyone advice me?

Recommended Answers

All 12 Replies

What you want is the size of an element of the array, which is not sizeof(typeid(arr)). You need to use sizeof(arr[0]). Don't worry about if arr[0] exists or not, because the sizeof operator does not actually evaluate the expression, only its type.

Warning: The method that you are using, i.e., sizeof(arr) / sizeof(arr[0]), will only work for a static array, not for a dynamically allocated array.

heres the array:
int b[5]={20, 30, 40, 2};

length=sizeof(arr) / sizeof(arr[0]);

why i don't get 5, but 1?

if you want to calculate the number of elements in string then follow this:

void main()
{
    int length=0;  
    string arr;
    cout<<"Enter your string\n";
    getline(cin,arr);   //takes the user input//

    length=arr.size(); 
    cout<<"The number of elements in the string are "<<length<<endl;




}

thanks but for a numeric type?

To answer the question in the title: There's no way to find out the number of elements in an array if you don't know its type, but it's also not possible not to know the type of an array unless you only have a (void or otherwise casted) pointer to the array. And if you only have a pointer, you can't get the size anyway (regardless of whether or not you know the type).

It's not possible to get the size of an object from its type's typeinfo object.

heres the array:
int b[5]={20, 30, 40, 2};
length=sizeof(arr) / sizeof(arr[0]);

What's arr? You only showed us the definition of b.

why i don't get 5, but 1?

Presumably because arr is just a pointer, not an array, but we'd have to see the definition of arr to know for sure.

@UFOOOOOOOOOOO: That doesn't help you get the number of elements in an array though, which is what the OP wanted.

template <typename T>
void print_array(T arr[])
{

    int length;//for recive the array size

    length=sizeof(arr) / sizeof(arr[0]);


    cout << length << endl;
    for (int i = 0; i < length; i++)
    {
        std::cout << arr[i] << " ";
    }
    std::cout << "\n";
}



int main()
{
    string a[6]={"joana", "João", "Manuel", "Zé", "Ana"};
    int b[5]={20, 30, 40, 2};
    int c[7]={50,2,60,1,30,0,53};
    print_array(b);

    return 0;
}

In a parameter list T arr[] is the same T* arr, so arr is a pointer. The only way to pass an array to a function is by reference. To declare an array reference you'd write T (&arr)[42] where 42 is the size of the array. In this case you'd probably want it to work with any size of array, so you'll need to make the size a template parameter like this:

template <typename T, size_t N>
void print_array(T (&arr)[N]) {
    ...
}

Now you don't even need to use the sizeof trick to get the length of the array, you can just use N.

The downside of this approach is that you'll only be able to use the method with variables of actual array types, so you won't be able to use it with arrays that have been allocated using new (as those would only be accessible through pointers - not directly through a variable). Of course that is true when using the sizeof trick as well.

If you want your function to be able to work with newed arrays too (or generally with arrays that you only have a pointer to), your only option is to take a pointer as your first argument and the size as the second.

something that i leraned today hehehe
thanks for all
but me ask 1 thing that i don't(i have read about templates, but the tutorials are so good): the template have 2 parameters, but you transform 'N' in second parameter and you don't use comma. can you explain to me these part, please?

the template have 2 parameters, but you transform 'N' in second parameter

What do you mean by "transform" here? I take N as the second template parameter.

you don't use comma

There's a comma between typename T and size_t N - where else do you expect a comma?

Just to clarify: There are two template parameters (T and N) and one function parameter (arr, which has type (T&)[N], i.e. "reference to an N-element array of Ts"). The template parameters are separated by commas. The function parameter is not as there's only one.

i thot the comma was in function parameters, but you don't use it.. you use "[N]". that's why i ask;)
is these that i ask for you explain to me;)
but seems obvious. thanks for all

As I said there's only one function parameter, so no commas. If you write soemthing like int arr[42], you don't have a comma in there either.

thanks for all.. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.