Tamanho da literal da string
O seguinte código
#include <iostream>
using namespace std;
int main()
{
const char* const foo = "f";
const char bar[] = "b";
cout << "sizeof(string literal) = " << sizeof( "f" ) << endl;
cout << "sizeof(const char* const) = " << sizeof( foo ) << endl;
cout << "sizeof(const char[]) = " << sizeof( bar ) << endl;
}
saídas
sizeof(string literal) = 2
sizeof(const char* const) = 4
sizeof(const char[]) = 2
em um sistema operacional de 32 bits, compilado com o GCC.
Porquesizeof
calcular o comprimento de (o espaço necessário para) a string literal?A string literal tem um tipo diferente (de char * ou char []) quando dado asizeof
?