Reputation: 48
Hi i am trying to get the length of unsigned char*, it is a pointer to a array witch means i cant use sizeof. I cant cast it to char* then do strlen() either, seeing as it contains null bytes in the middel of the string. What other way is there to do this?
stream.open("test.txt");
stream.write((char*)c, Length of the string); // How do i find Length of the string
stream.close();
Upvotes: 0
Views: 254
Reputation: 17454
tl;dr: You can't.
Hi i am trying to get the length of unsigned char*
Presumably, you mean the length of the array it points to.
it is a pointer to a array witch means i cant use sizeof.
That's right.
I cant cast it to char* then do strlen() either, seeing as it contains null bytes in the middel of the string.
Okay, so it's not a null-terminated C-string. Null termination is the way to calculate a C-string's length when you haven't stored it by other means.
What other way is there to do this?
None. You'll have to store it by other means. For example, pass it around with the pointer.
Upvotes: 1
Reputation: 238351
it is a pointer to a array
To use precise terminology: It is a pointer to an unsigned char. That unsigned char may be an element of an array.
i cant use sizeof. ... it contains null bytes in the middel of the string. What other way is there to do this?
Seeing as you know that there is an array, the solution is simple: use sizeof
on the array instead of the pointer.
Upvotes: 0