raketmus
raketmus

Reputation: 48

Get length of unsigned char*

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

Answers (2)

Asteroids With Wings
Asteroids With Wings

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

eerorika
eerorika

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

Related Questions