getting the length of an array after passing as a char*

arrays, c++, char, pointers

Solution

C style arrays do not carry length information with them.

You have several solutions avaliable :

pass the size of your array to your function :

void arrLen(char* array, std::size_t size)

use std::array if you have access to C++ 11 (or TR1 as mentionned in comments)

 std::array<char, 16> bytes;

If you don't have access to std::array, you can use boost::array. They are roughly equivalent to the standard one.

Regarding your comment

would I have to pass it as a char[] instead of a char*?

Arrays are always passed to functions as pointers, so `void arrLen(char* array)` and `void arrLen(char[] array)` are equivalent.

Problem

Possible Duplicate: Sizeof an array in the C programming language? I have code of the following form: ``` void arrLen(char* array){ // print length } int main(){ char bytes[16]; arrLen(bytes); } ``` What I need to do is get the length of the underlying array and print it out. The issue is that the array MAY CONTAIN NULL CHARACTERS, and therefore `strlen` would not work. is there any way to do this, or would I have to pass it as a `char[]` instead of a `char*`?

Original source

Related problems