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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With