char
.
std::string
(“C ++ strings”) and const char*
(C strings, which must be maintained for compatibility).byte
, it is constructed through the type char
. The Standard made a special clarification that the types char
, signed char
and unsigned char
are three different types. Other integral types do not have this property, for example, int
and signed int
are identical definitions. An additional rake here is the fact that the type char itself must be either signed or unsigned - it depends on the platform (roughly speaking, the compiler and its key set). But at the same time, the compiler is still obliged to distinguish them all from each other.
void foo(char c); void foo(signed char c); void foo(unsigned char c);
#include <iostream> #include <stdint.h> int main() { uint8_t b = 42; std::cout << b << std::endl; // *, 42. }
char*
(or const char*
) pointer, then this is most likely the string, and it can be passed to the corresponding functions. Plain C allows even such amazing things as, for example:
int main(void) { char* ptr = "hello"; // C, ptr[1] = 'q'; // "abcd"[1] = '2'; // - , - read-only, return 0; }
The good news is that in C ++ this feature was not transferred.
"abc\0\123"
), and functions that are designed to work with them ( strlen
, etc.) do not support such strings. That is, due to the decision that “all lines are a sequence of non-zero characters ending in zero” immediately got a situation that not all the lines made it possible to work and funny effects like complexity O (n) for such an operation as “get the length given lines. "
'\0'
for all string literal, this leads to the following consequences:
char str1[] = "1234"; // 5 , 4 char str2[4] = "1234"; // , char str3[4] = {'1', '2', '3', '4'}; // ...
char*
, that is, you can pass it into puts
, strlen
, etc. and get undefined behavior.
Source: https://habr.com/ru/post/154033/