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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40

5 Answers5

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.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
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.

john
  • 85,011
  • 4
  • 57
  • 81
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.

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • **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 *`. – Happy Green Kid Naps Apr 02 '13 at 21:56
  • @HappyGreenKidNaps You're right. That was my oversight here. – milleniumbug Apr 02 '13 at 22:21
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;
}
jxh
  • 69,070
  • 8
  • 110
  • 193
  • 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 answer – stefanB Apr 02 '13 at 22:29
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)

Sirmyself
  • 1,464
  • 1
  • 14
  • 29