0

Can you say me why different size in this example:

// 1
std::string str = "Sun Mar 29 16:05:28 2020";
const unsigned char *t{};
t = (unsigned char *) str.c_str();

std::cout << "t: " << t << " : " << sizeof(t) << " : " << str.length() << std::endl;
std::cout << "--" << std::endl;

// 2
const unsigned char f[] = "Sun Mar 29 16:05:28 2020";
std::cout << "f: " << f << " : " << sizeof(f) << std::endl;
std::cout << "--" << std::endl;

Output:

t: Sun Mar 29 16:05:28 2020 : 8 : 24

f: Sun Mar 29 16:05:28 2020 : 25

mat.twg
  • 438
  • 1
  • 5
  • 11
  • 1
    [`c_str`](https://en.cppreference.com/w/cpp/string/basic_string/c_str): "Returns a pointer to a null-terminated character array with data equivalent to those stored in the string." Length only counts the characters not the null-terminator – 463035818_is_not_an_ai Mar 29 '20 at 16:17
  • 1
    Your `f` and `t` variables are fundamentally different types! `f` is declared as an array of size 25 characters (24 visible plus the `nul` terminator), whereas `t` is declared as a pointer to `char` data (and, on your system, a pointer has a size of 8 bytes). – Adrian Mole Mar 29 '20 at 16:18

0 Answers0