0

I am not able to use int8_t* type inside the strlen() function, it gives error like

invalid conversion from int8_t* to const char*`

Please help me, how to resolve this issue?

If I change the type from int8_t* to char* then it is working fine, but I can't use char* as per my project coding guidelines I have to use either int8_t or uint8_t, so please help me to resolve this issue.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
sokid
  • 813
  • 3
  • 10
  • 16
  • 1
    Is this a duplication? http://stackoverflow.com/questions/12381855/int8-t-and-char – Dr Rob Lang Jul 19 '13 at 10:31
  • I think it is not duplicate, I want to know, is there any other method similar like strlen() for int8_t types? – sokid Jul 19 '13 at 10:36
  • You either must cast or must write your own version of `strlen` that operates on a sequence of `int8_t` elements. – jamesdlin Jul 19 '13 at 10:44
  • How to cast from "int8_t*" to "const char*"? – sokid Jul 19 '13 at 10:48
  • Can you post the code you are trying? Are you trying to send an int8_t into strlen? – doctorlove Jul 19 '13 at 10:59
  • @sokid Do your guidelines actually say never to use `char` or not to use `char` for things that aren't conceptually *characters*? *Never* using `char` is ridiculous. Your other question ( http://stackoverflow.com/questions/17744226/int8-t-vs-char-which-is-the-best-one ) makes me think it's something like the latter. – jamesdlin Jul 19 '13 at 11:12

2 Answers2

0

The "solution" to this is one of three things:

  1. Use char instead of int8_t.
  2. Write your own strlen type function.
  3. Cast int8_t to char at the callsite.

The cast solution would look something like this:

int8_t something[100];
... something gets set somehow ... 
size_t len = strlen((char *)something); 

or in C++:

size_t len = strlen(static_cast<char *>(something)); 

Personally, I think option 1 is the "right" one if the data being processed is really string data. That is what the char type is for, but not what int8_t is for.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

Don't use int8_t unless it meets your requirements. It is not a synonym for char. It is a signed integral type that has exactly 8 bits, and it won't exist on platforms that don't have 8-bit integral types. When you want to deal with char objects, use the type char. That's what it's for.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165