How to get the length of an array from a pointer? [duplicate]
Asked Answered
T

5

21

I'm having trouble finding the length of an pointer array. Let's say I have:

char array[40] = "hello"; // size 40
int length =  sizeof array / sizeof array[0]; // no problem returns 40

//How do I get the length of the array with only a pointer to the first element in that array?

 char* pchar = array;

//if

std::strlen(pchar); // this returns the length of 5... i want length 40

//if

int count = 0;
while(true)
{
  if(*(pchar + count) == '\0') // returns 5...
    break;
   count++;
}

How do I get it to return length 40 just from a pointer to the first element in the array?
I found that I can do this.

int count = 0;
    while(true)
    {
      if(*(pchar + count) == '\0' && *(pchar + count + 1) != '\0') 
             break;

       count++;
    }

This returns 39, this is good but I feel like this can be buggy in some situations.

Trousers answered 2/4, 2013 at 21:16 Comment(2)
You can't get the length of an array from a pointer.Krys
There are hacks, but there is no builtin way to do this.Kiangsu
M
17

You can't, I'm afraid. You need to pass the length of the array to anyone who needs it. Or you can use a std::array or std::vector or similar, which keep track of the length themselves.

Moorings answered 2/4, 2013 at 21:17 Comment(2)
hmm but my last block of code returned 39, +1 = 40. why is this bad?Trousers
@JavaNewb: You're reading off the end of the array into undefined memory, which is likely to cause a crash.Moorings
R
5

You can't. And the moral is, don't use pointers and arrays, use vectors. You can always get the size of a vector.

Raisin answered 2/4, 2013 at 21:19 Comment(0)
L
5

C++ has proper string type:

std::string

which you may find helpful here. Even if you're passing it to function that accepts const char*, it has .c_str() method that allows you to pass it to function that accept a pointer. If the other function needs to modify the string, you can use &str[0] which is valid for many implementations of C++, and is required to work for C++11. Just make sure you resize() them to the correct size.

Some of the other containers in C++ are:

std::array (C++11) Array of constant size. Better than plain old C array, as it has size() method. std::vector Dynamic array (Java ArrayList equivalent)

As for your question - there is no way to find size of a pointed array. How could you even do that? It's just a stupid pointer.

Lilongwe answered 2/4, 2013 at 21:33 Comment(2)
Even if you're passing it to function that accepts char*, it has .c_str() method that allows you to pass it to function that accept a pointer. ==> That is incorrect. The c_str() member function returns a const char *.Zygodactyl
@HappyGreenKidNaps You're right. That was my oversight here.Lilongwe
D
4

It is true that you cannot get the array size from a pointer to an element of the array.

If the reason you only have a pointer is because you are implementing a function that takes an array parameter as an argument like this:

void foo (T *p) {
    // p is a pointer to T
}

then you can use a template function instead to get the array size to the function.

template <unsigned N, typename T>
void foo (T (&p)[N]) {
    // p is a reference to an array[N] of T
    std::cout << "array has " << N << " elements" << std::endl;
    std::cout << "array has "
              << sizeof(p)/sizeof(p[0])
              << " elements"
              << std::endl;
}

int main ()
{
    int array[40];
    char array2[25];
    foo(array);
    foo(array2);
    return 0;
}
Debt answered 2/4, 2013 at 21:23 Comment(1)
+1, yes, so the full answer would be: you can't get the array size from the pointer to an array but if it's an option instead of passing pointer to an array to a function you could pass reference to an array and then you can get the size using code from this answerUuge
T
1

If you want to keep simple and not use #include <array.h>, you can make a struct where you array sizes are. A little bit like:

struct
{
    int* int_Array;
    int array_Size;
}

That way, you can to whatever you want (like deleting your instances or something)

Tenner answered 20/10, 2015 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.