How to get the length of an array from a pointer?
arrays, c++, pointers
Solution
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.
Problem
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.