The following code
#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;
}
outputs
sizeof(string literal) = 2
sizeof(const char* const) = 4
sizeof(const char[]) = 2
on a 32bit OS, compiled with GCC.
- Why does
sizeof
calculate the length of (the space needed for) the string literal ?
- Does the string literal have a different type (from char* or char[]) when given to
sizeof
?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…