3

Why does the statement const int8_t* cstr = "asdf"; gives error

invalid conversion from ‘const char*’ to ‘const int8_t*’

Aren't int8_t* and char* same? Am I missing some subtle thing here?!

zeropoint
  • 235
  • 1
  • 4
  • 10
  • 2
    They may behave the same, but they're still treated as different types. Furthermore, the signness of `char` is not specified and can be either depending on what the implementation chooses. – Mysticial Sep 12 '12 at 05:45

2 Answers2

6

const signed char* is not the same as const char*. Check your compilation settings, because that would explain it. int8_t is always (never say never =) at least everywhere i've seen) defined as signed char.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 3
    `signed char` is **never** the same as `char`. They're different types, always. – Kerrek SB Sep 12 '12 at 05:50
  • But doesn't `char` have to be either `signed` or `unsigned` (for `unsigned` it's understood as we explicitly write `unsigned char`) Then, is there any programmatic significance of such differentiation -- because anywhere either `signed char` or `unsigned char` would suffice for any program? – zeropoint Sep 12 '12 at 06:00
  • 2
    @zeropoint `char` will be signed or unsigned, but will still be a distinct type from `signed char` and `unsigned char` either way. This has the same significance as distinct types always do in C++. – bames53 Sep 12 '12 at 06:16
  • 1
    char, signed char, and unsigned char are most-definitely different types and will/should be treated as such unless you're compiler is bending the standard to be 'helpful' (loose term). MS' compiler, for example, allows configuration to treat all 'char' as 'signed char' or 'unsigned char', and while that sounds like a stellar idea, it has a most-troublesome side effect of introducing a plethora of compilation errors when porting to a compiler that actually does what it is supposed to; follow the standard. – WhozCraig Sep 12 '12 at 06:20
  • Thank you @Craig Nelson for in-depth clarification – zeropoint Sep 12 '12 at 09:17
4

According to [18.4 Integer types]:

typedef signed integer type int8_t; // optional

And [3.9.1 Fundamental types]:

Plain char, signed char, and unsigned char are three distinct types

int8_t is a signed integer type (on my system defined as signed char) and char and signed char are distinct types, so they are different.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166