plz tl me how to find length of integer array

Dani AI

Generated

Short answer: how you get an array length in C++ depends on what you actually have. For a built‑in, fixed C array the “sizeof trick” (mentioned by and shown in other replies) only works when the array’s identifier is visible at that point in the code — the array’s size is part of its type. If the array has decayed to a pointer (for example, when passed to a function or when you only have an int*), the size information is lost and sizeof will give the size of the pointer, not the element count. (en.cppreference.com)

Prefer modern, self-describing containers when possible. std::vector (dynamic) and std::array (fixed-size, non-decaying wrapper) both provide .size() and are the idiomatic, safe choices in C++. Example usage:

#include <vector>
#include <array>

std::vector<int> v{1,2,3};
auto nVec = v.size();

std::array<int, 3> a{1,2,3};
auto nArr = a.size();

These containers are documented in the standard library. (en.cppreference.com)

If you must work with C-style arrays in APIs, either pass the size along, accept the array by reference (or use a function template that deduces the length), or use std::span (C++20) to accept a contiguous view while preserving .size(). Example patterns:

template<typename T, std::size_t N>
constexpr std::size_t array_length(T (&)[N]) noexcept { return N; }

#include <span>
void process(std::span<int> s) {
    auto n = s.size();
}

std::size (since C++17) also provides a generic way to get sizes for arrays and containers. (cs.helsinki.fi)

Quick troubleshooting: dynamic allocations (new int[n]) do not store n — you must track it. strlen/string tricks (as proposed by ) apply only to char strings and are wrong for int arrays; raw arrays have no .length() member (so ’s suggestion isn’t C++). When in doubt, use std::vector/std::array or std::span for clear, maintainable code. (en.cppreference.com)

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.