Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
What should be the function to get length of array in variables len1 and len2, i am not able to get the length of array through the function length() ,why is it?

C++
#include<iostream.h>
#include<conio.h>

template <class X>
class MyClass 
{
void disparray(X *p,int n)
{
     int i;
     for(i=0;i<n;i++)>
            cout<<p[i]<<" ";
}

int main()
{
    int a[]={1,2,3,4};
    float b[]={1.1,2.2,3.3};
    int len1,len2;
    len1=a.length();
    len2=b.length();
    disparray(a,len1);
    disparray(b,len2);
    getch();
    return 0;
}
Posted
Updated 15-Aug-11 7:54am
v4

The length of an array is set when it is declared. For Example:

int a[10];
Declares an array of 10 ints. Or:

int a[] = {1,2,3};
Declares an array of 3 ints which contain 1, 2 and 3.

I think perhaps you are thinking of the class CArray, which can dynamically change its size. Here's the link to the Microsoft help for CArray:

http://msdn.microsoft.com/en-us/library/4h2f09ct(v=vs.80).aspx[^]
 
Share this answer
 
v2
Comments
Ashutosh_g 15-Aug-11 3:49am    
no sir i am just asking about any function as length() exists in c++ to get the length of array?
LittleYellowBird 15-Aug-11 4:17am    
Hi, A word of advice, I am NOT a sir, it is best not to assume that all programmers are men! You do not need to use length(), it has no meaning for you, since YOU set the length of the array when you declared it. I think you need to find a good reference book and read more about arrays, so that you have a better understanding. Best Regards :)
Manfred Rudolf Bihy 15-Aug-11 13:34pm    
Well said Sir!

errhhmm, Madam Sir! ;)
LittleYellowBird 16-Aug-11 0:59am    
Hehe! :)
Sergey Alexandrovich Kryukov 15-Aug-11 22:58pm    
Can I up-vote this, too? (My 5 for the solution.)
--SA
I think you need to go back to your C++ reference guides and learn the difference between simple types and classes/structs. The arrays you have defined are simple types and do not have member functions in the way that classes or structs have. The only way to get the length of a simple type array is to use the sizeof operator, but this is resolved at compile time so will probably not suit your purpose. You probably need to use one of the STL classes[^] that handle collections, probably list or vector. You should also read up on the syntax of Templates[^], as your specification is incorrect.

[edit]I should also mention _countof() (defined in stdlib.h) which gives the number of elements rather than the total size. Also ARRAYSIZE (defined in WinNT.h).[/edit]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Aug-11 23:00pm    
Oh, yes, learning of arrays and beyond is really required here; my 5.
--SA
Richard MacCutchan 16-Aug-11 4:18am    
Thanks SA.
For a standard C++ array you can use this to find out it's number of elements:
sizeof(arr)/sizeof(arr[0])
Here, arr is the array you declared. It basically means the size of the total array divided by the size of each element.
Though I don't suggest this approach in general.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 15-Aug-11 13:36pm    
I don't know if this is good or bad style, but I've seen it done this way quite often back when I was programming C up to the mid 90's. Take my 5!
Robin Cheng (HPMV) 15-Aug-11 13:47pm    
Ah I see; didn't know that before :)
I say it's a bad style because if we have C++ we have a greater flexibility of doing things. If we need a fixed sized array and want to vary the size at compile time, we could use a const int to specify the size which can later be accessed. If we need a variable sized array we could use classes like std::vector.
Personally I would suggest this only if it's out of pure convenience and used for a local array. Because this is a compile time computation and only works if the length of arr is known at compile time. This means if an array is passed to a function, the function would not know this and the aforementioned expression would not work.
Robin Cheng (HPMV) 15-Aug-11 13:49pm    
Oh and I guess it's also useful for arrays declared without explicit bound with an array literal, like in te original question :)
Richard MacCutchan 15-Aug-11 14:02pm    
See latest updates in my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900