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*?
C style arrays do not carry length information with them.
You have several solutions avaliable :
pass the size of your array to your function :
use std::array if you have access to C++ 11 (or TR1 as mentionned in comments)
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
Arrays are always passed to functions as pointers, so
void arrLen(char* array)andvoid arrLen(char[] array)are equivalent.