int main() { auto s = "Hello...">

Why is auto in `auto s =" abc "` yield a char pointer instead of a char array?

This program

#include <iostream>

int main() {
    auto s = "Hello, world!\n";
    decltype("Hello, world!\n") t = "Hello, world!\n";
    std::cout << sizeof s << ", ";
    std::cout << sizeof t << ", ";
    std::cout << sizeof "Hello, world!\n" << '\n';
    return 0;
}

prints

4, 15, 15

This suggests that the type sis equal char *. This seems stranger, given that the type of the string literal tis equal char [15].

Why does this program not print 15, 15, 15instead?

Why autodoes it declare a pointer type instead of an array type when assigned to a string literal?

+4
source share

Source: https://habr.com/ru/post/1626775/


All Articles