plz tl me how to find length of integer array

Recommended Answers

All 8 Replies

int x[5];
Length is: sizeof(x)/sizeof(int);

using

len = array.length( );

functionnn

using

len = array.length( );

functionnn

Correct me if I'm wrong, but length() works for strings not integer arrays. If it does work then please demonstrate how, because I got a syntax error trying it.

Here's some code that I know works. It shows 2 ways of getting the length. Wingless' answer is correct as shown bellow.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int myArray[] = { 1, 2, 3, 4, 5};

    int length1 = sizeof myArray / sizeof *myArray;
    int length2 = sizeof myArray / sizeof(int);

    cout << length1 << endl;
    cout << length2 << endl;

    return EXIT_SUCCESS;
}

As Crazyboy says, use the following:

string str (ARRAYNAME);
SomeLengthVar=str.length();

or simply use:

strlen(ARRAYNAME));

That's about the easiest way

Correct me if I'm wrong, but length() works for strings not integer arrays.

You're so right, that's why I used:

string str (ARRAYNAME);
      SomeLengthVar=str.length();

In my example, the string str (ArrayName), makes a string called str, of the array, and then feeds that to the ".length()"

using

len = array.length( );

functionnn

As Crazyboy says, use the following:

string str (ARRAYNAME);
SomeLengthVar=str.length();

or simply use:

strlen(ARRAYNAME));

That's about the easiest way

Did you read the question? He's asking for how to get the length of an integer array, not a string.:P strlen(MyIntArray) is just so not going to do what the poster wants.

int a[] = {1,2,3,4,5,6};
int n = sizeof(a) / sizeof(a[0]); //returns Size of the array

Did you read the question? He's asking for how to get the length of an integer array, not a string.:P strlen(MyIntArray) is just so not going to do what the poster wants.

I did read the question, however it seems like my brain failed, since I was thinking about it as a char array >.<'.

But if it's a integar array, then the right way to do it, is ofc. to do the:

(sizeof myArray / sizeof *myArray);

And I'm sorry for the confusion I made

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.