某32位系统下, C++程序,请计算sizeof 的值.

来源:互联网 发布:知牛财经直播违法吗 编辑:程序博客网 时间:2024/05/16 09:29

32位系统下, C++程序,请计算sizeof 的值.

void Foo_1 ( char str[100]){

cout<<"Foo_1:"<<sizeof( str )<<endl; //

}

  char str_1[] = "abcdef"; 

cout<<"str_1:"<<sizeof(str_1)<<endl; // 7 加了一个‘\0’ 

char *str_2 = "abcdef"; 

cout<<"str_2:"<<sizeof(str_2)<<endl; // 4 

char str_3[] = {'a','b','c','d','e','f'}; 

cout<<"str_3:"<<sizeof(str_3)<<endl; // 6 

// char *str_3 = {'a','b','c','d','e','f'}; 编译错误

// char *str_3 = {'a'}; 编译错误

char *p = str_1 ;

cout<<"p:"<<sizeof(p)<<endl; // 4 

char *q = str_2 ;

cout<<"q:"<<sizeof(q)<<endl; // 4 

char *l = str_3 ;

cout<<"l:"<<sizeof(l)<<endl; // 4 

Foo_1(str_1);  // 4 

Foo_1(str_2);  // 4 

Foo_1(str_3);  // 4 

void *k = malloc( 100 );

cout<<"k:"<<sizeof(k)<<endl; // 4

附:数组做形参,则退化为指针;数组的引用做形参,则数组的大小也成为形参类型的一部分。

原创粉丝点击