how to calculate the size of the string with /0 inside which a pointer points to

135 Views Asked by At

let say I have a char pointer like this:

unsigned char* value="Hello\0Hello";

how can i calculate the size of value, as strlen only calculate the size of string until the '/0' so I can not use strlen, and also sizeof returns the length of the pointer itself so is there any way to calculate the length of the value?

1

There are 1 best solutions below

3
Remy Lebeau On

how can i calculate the size of value ... the structure of my program is in a way that i can not use array and it should be pointer.

There is no built-in function for this scenario.

The only possibility to calculate the string length is if either:

  • the string is double null-terminated, eg: Hello\0Hello\0\0

  • if you know in advance how many intermediate null characters exist in the string before the final null terminator.

In either case, you can then enumerate the string, counting the chars and skipping the intermediate nulls, until you reach the final null terminator.

If neither of those cases is true in your situation, then you are out of luck unless you have an out-of-band way to know the string's length.