You will have to pass the length of the data back from MyFunction. Also, make sure you know who allocates the memory and who has to deallocate it. There are various patterns for this. Quite often I have seen:
int MyFunction(unsigned char* data, size_t* datalen)
You then allocate data and pass datalen in. The result (int) should then indicate if your buffer (data) was long enough...
Now this is really not that hard. You got a pointer to the first character to the string. You need to increment this pointer until you reach a character with null value. You then substract the final pointer from the original pointer and voila you have the string length.
int strlen(unsigned char *string_start)
{
/* Initialize a unsigned char pointer here */
/* A loop that starts at string_start and
* is increment by one until it's value is zero,
*e.g. while(*s!=0) or just simply while(*s) */
/* Return the difference of the incremented pointer and the original pointer */
}
As said before strlen only works in strings NULL-terminated so the first 0 ('\0' character) will mark the end of the string. You are better of doing someting like this:
unsigned int size;
unsigned char* data = MyFunction(&size);
or
unsigned char* data;
unsigned int size = MyFunction(data);
HTH
There is no way to find the size of (
unsigned char *)
if it is not null terminated.Assuming its a
string
but it doesn't seem to be...so there isn't a way without having the function return the length.
You will have to pass the length of the data back from
MyFunction
. Also, make sure you know who allocates the memory and who has to deallocate it. There are various patterns for this. Quite often I have seen:You then allocate data and pass datalen in. The result (int) should then indicate if your buffer (data) was long enough...
Now this is really not that hard. You got a pointer to the first character to the string. You need to increment this pointer until you reach a character with null value. You then substract the final pointer from the original pointer and voila you have the string length.
As said before strlen only works in strings NULL-terminated so the first 0 ('\0' character) will mark the end of the string. You are better of doing someting like this:
or