Size_t被定义为一个unsigned整数,但它的大小取决于你是在32位还是64位机器上.什么是正确和便携的打印方式size_t?
class Simple {
string *data ;
//some functions and declaration of some variable,const, dest
};
Run Code Online (Sandbox Code Playgroud)
我什么时候做的;
data = new string [10] ;
cout << sizeof data / sizeof *data << endl ;
==> 1 // output
data = new string [50];
cout <<sizeof data / sizeof *data <<endl ;
==> 1 // output
**Why** do all output display the same ?
Run Code Online (Sandbox Code Playgroud) Sizeof打印出6:
printf("%d\n", sizeof("abcde"));
Run Code Online (Sandbox Code Playgroud)
但它打印出4:
char* str = "abcde";
printf("%d\n", sizeof(str));
Run Code Online (Sandbox Code Playgroud)
有人可以解释原因吗?
可能重复:
C - > sizeof字符串始终为8
如果我这样做,sizeof("HELLO");我会得到6,这是预期的,但是如果我有:
void printLength(char *s) {
sizeof(s);
}
Run Code Online (Sandbox Code Playgroud)
传递"Hello"到此函数sizeof返回8.
为什么是这样?