关于华为的一道笔试题

mxmkeep 2008-11-30 06:46:00
你好!
这是我在网站上看到的华为笔试题,不明白,大家能给我解释下么?
2.某32位系统下, C++程序,请计算sizeof 的值(5分).
char str[] = “iiren”
char *p = str ;
int n = 10;
请计算
sizeof (str ) = ?(1)
sizeof ( p ) = ?(2)
sizeof ( n ) = ?(3)
void Foo ( char str[100]){
请计算
sizeof( str ) = ?(4)
}
void *p = malloc( 100 );
请计算
sizeof ( p ) = ?(5)
答:(1)5 (2)4 (3) 4 (4)4 (5)4
本文来源于<a href=http://www.izuren.com>IIREN面试集</a>


我的问题:
1:(1)的答案不是6么?包含结束字符啊..
2:(2)、(4)、(5)不懂,能解释下么?
3:我编写的代码在vc6中编译不了,请问该怎么改?(下面)
#include<iostream.h>
void main()
{
char str[]="iiren";
char* p=str;
int n=10;

cout<<"(1):sizeof(str)="<<sizeof(str)<<endl;
cout<<"(2):sizeof(P)="<<sizeof(P)<<endl;
cout<<"(3):sizeof(n)="<<sizeof(n)<<endl;

void Foo(char str[100])
{
cout<<"(4):sizeof(str)="<<sizeof(str)<<endl;
}

void* p=malloc(100);
cout<<"(5):sizeof(P)="<<sizeof(P)<<endl;
}

...全文
203 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
mxmkeep 2008-11-30
  • 打赏
  • 举报
回复
谢谢楼上的回答!
谢谢你,我大部分已经懂了..
ken_scott 2008-11-30
  • 打赏
  • 举报
回复
程序修改后如下:
#include<iostream.h>
#include<stdlib.h>
void Foo(char str[100])
{
cout<<"(4):sizeof(str)="<<sizeof(str)<<endl;
}
void main()
{
char str[]="iiren";
char str2[100]="klaflasflkasflkalkfklaf";
char* p=str;
int n=10;
cout<<"(1):sizeof(str)="<<sizeof(str)<<endl;
cout<<"(2):sizeof(p)="<<sizeof(p)<<endl;
cout<<"(3):sizeof(n)="<<sizeof(n)<<endl;
void* pp=malloc(100);
cout<<"(5):sizeof(pp)="<<sizeof(pp)<<endl;
free(pp);
Foo(str2);
}


运行结果:6 4 4 4 4 4
ken_scott 2008-11-30
  • 打赏
  • 举报
回复
修改一下第3个
因为是int在32位系统下是占4字节,(16位下是2个字节)
所以是4

不好意思,没看清!
liaoweixiaoyu 2008-11-30
  • 打赏
  • 举报
回复
lz 的都答对了啊
mxmkeep 2008-11-30
  • 打赏
  • 举报
回复
谢谢,马上结贴,你们回答的太快了,我都谢不完... ^_^
mxmkeep 2008-11-30
  • 打赏
  • 举报
回复
很高兴这么快就有这么多人回答了,谢谢大家的回答和指出的错误!
谢谢Demon__Hunter xyx119 fox000002 nullah wuyu637
谢谢你们
ken_scott 2008-11-30
  • 打赏
  • 举报
回复
1.sizeof和strlen不一样,但如楼主所说sizeof下是6,而strlen下是5;可知答案是错的,上机运行一下就知道了!
2.-4.由于都是指针,所以在sizeof下都是4,只要是指针,sizeof下都是4;
5.由于数组做函数参数时会自动退化为指针类型,由上知为4.
mxmkeep 2008-11-30
  • 打赏
  • 举报
回复
感谢Demon__Hunter 的回答!
这么说(1)的答案是6咯,他们给的答案是错的?
再次谢谢Demon__Hunter 的回答和解释..
谢谢!
wuyu637 2008-11-30
  • 打赏
  • 举报
回复
sizeof(P) ===========》 sizeof(p)
nullah 2008-11-30
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;

int main()
{
char str[]="iiren";
char *p=str;
int n=10;

cout<<"(1):sizeof(str)="<<sizeof(str)<<endl;
cout<<"(2):sizeof(P)="<<sizeof(p)<<endl;
cout<<"(3):sizeof(n)="<<sizeof(n)<<endl;

void Foo(char str[]);

char str2[100] = "123456789";
Foo(str2);

void *b=malloc(100);
cout<<"(5):sizeof(P)="<<sizeof(b)<<endl;

return 0;
}

void Foo(char str[])
{
cout<<"(4):sizeof(str)="<<sizeof(str)<<endl;
}


楼主程序有点问题
修改了下
output:
(1):sizeof(str)=6
(2):sizeof(P)=4
(3):sizeof(n)=4
(4):sizeof(str)=4
(5):sizeof(P)=4
请按任意键继续. . .

1的答案就是6
具体请看
http://www.vckbase.com/document/viewdoc/?id=1054
fox000002 2008-11-30
  • 打赏
  • 举报
回复
第一个的确是 6

sizeof n == sizeof int

其他的都是指针,32 位的话为 4

#include <iostream>
#include <string.h>
using namespace std;

void Foo(char str[100])
{
cout<<"(4):sizeof(str)="<<sizeof(str)<<endl;
}

int main()
{
char str[]="iiren";
char *p=str;
int n=10;

cout<<"(1):sizeof(str)="<<sizeof(str)<<endl;
cout<<"(2):sizeof(P)="<<sizeof(p)<<endl;
cout<<"(3):sizeof(n)="<<sizeof(n)<<endl;

char ss[100];
Foo(ss);

void *pp=malloc(100);
cout<<"(5):sizeof(P)="<<sizeof(pp)<<endl;

return 0;

}

xyx119 2008-11-30
  • 打赏
  • 举报
回复
1:(1)的答案不是6么?包含结束字符啊..
这个答案错了,应该是4,这个要搞清楚sizeof 和strlen的区别就明白了
2:(2)、(4)、(5)不懂,能解释下么?
指针是个DWORD,占四个字节,不管他指向什么东西,他都是4个字节。

xyx119 2008-11-30
  • 打赏
  • 举报
回复
1:(1)的答案不是6么?包含结束字符啊..
这个答案错了,应该是4,这个要搞清楚sizeof 和strlen的区别就明白了
2:(2)、(4)、(5)不懂,能解释下么?
指针是个DWORD,占四个字节,不管他指向什么东西,他都是4个字节。

机智的呆呆 2008-11-30
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
void Foo(char str[100])
{
cout<<"(4):sizeof(str)="<<sizeof(str)<<endl;
}
void main()
{
char str[]="iiren";
char* p=str;
int n=10;
cout<<"(1):sizeof(str)="<<sizeof(str)<<endl;
cout<<"(2):sizeof(p)="<<sizeof(p)<<endl;
cout<<"(3):sizeof(n)="<<sizeof(n)<<endl;
char str1[100]={0};
Foo(str1);
void* q=malloc(100);
cout<<"(5):sizeof(q)="<<sizeof(q)<<endl;
system("pause");
}


指针变量为win32平台为4个字节,数组做为函数参数时会退化为指针~~~

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧