继续贴题——找错题,可能不止一个错误,并改正

zhouliang0806 2011-06-09 05:26:49
1、void Greeting(const char*name)
{
if(name){
char buffer[128]={0};
strcpy(buffer,"Hello");
strncat(buffer,name,sizeof(buffer)-1);
printf("%s",buffer);}
}

2、void GetSum(unsigned int num)
{
int sum=0;
while(num>=0)
{
sum+=num--;
}
printf("The sum is 0x%x\n",sum);
}


3、void main(int argc,char** argv)
{
char c;
const int intone;
const int *pi;
pi=(int *)&c;
pi=0;
pi=&intone;
cont<<*pi<<endl;
}


4、void foo(void)
{
char string[10],str1[10];
int i;
for(i=0;i<10;i++)
{
str[i]='a';
}
strcpy(string,str1);
printf("%s",string);
}


5、struct Test
{
Test(int){}
Test(){}
void fun() {}
};
void main(void)
{
Test a(1);
a.fun();
Test b();
b.fun();
}


好了,欢迎大家热烈讨论
题目重述一遍:It is assumed there is only one defect within each program,but it will be great if you find more and figure out all of them.
...全文
83 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
guanzhouxuezi 2011-06-09
  • 打赏
  • 举报
回复
第一题:
看看strncat的原型:char * strncat ( char * dest, char * src, size_t n );该函数把src所指字符串的前n个字符添加到dest所指字符串的后面。所以应该是strncat(buffer,name,strlen(name));
第二题:
num是unsigned int类型,所以永远大于等于0,循环为无限循环。
第三题:
const int intone;未初始化
第四题:
strcpy函数的原型是这样的:

char *strcpy(char *dest,const char *src)
{
assert(dest!=NULL&&src!=NULL);
char *address = dest;
while(*(dest++)=*(src++));
return address;
}

错误代码是这样的:

for(i=0;i<10;i++)
{
str[i]='a';
}
strcpy(string,str);

一看就知道了,str字符串没有'\0',无法结束。
第五题:
Test b();
改为Test b;



zhouliang0806 2011-06-09
  • 打赏
  • 举报
回复
我写了一下,不知道对不:
1、strlen和sizeof的区别,如果是sizeof(buffer)=128,应该是strlen(buffer);
2、因为是无符号整数,那么小于0时是个很大的数,那么会出现死循环,改为num>0;
3、pi=0;我看好像错了,起码也应该是*pi=0啊;
4、越界了,只能有9个a,后面有一个'\0';
5、不知道
tompaz 2011-06-09
  • 打赏
  • 举报
回复
没注意第二个是Unsigned,那是不会小于0,死了

tompaz 2011-06-09
  • 打赏
  • 举报
回复
1.
strcpy(buffer,"Hello");
这句话会导致输入参数长度小于5的时候,输出不对
2.看不懂
3.应该没问题,因为char c在编译后由于对齐,会占用4个字节,不会冲突到下面的int
4.strcpy导致越界
5,不明白
一根烂笔头 2011-06-09
  • 打赏
  • 举报
回复

void Greeting(const char*name)
{
if(name){
char buffer[128]={0};
strcpy(buffer,"Hello");
strncat(buffer,name,sizeof(buffer)-1);
printf("%s",buffer);}
}
void GetSum(unsigned int num)
{
int sum=0;
while(num>=0)//死循环
{
sum+=num--;
}
printf("The sum is 0x%x\n",sum);
}
void foo(void)
{
char string[10],str1[10];
int i;
for(i=0;i<10;i++)
{
//str[i]='a';
str1[i]='a';
}
strcpy(string,str1);
printf("%s",string);
}
struct Test
{
Test(int){}
Test(){}
void fun() {}
};
int main()
{
//问题3
char c;
//const int intone;没有初始化
const int intone=100;
const int *pi;
pi=(int *)&c;
pi=0;
pi=&intone;
//cont<<*pi<<endl;
std::cout<<*pi<<std::endl;

//问题5
Test a(1);
a.fun();
//Test b();
Test b;
b.fun();

//问题4
foo();

//问题2
int num=10;
GetSum(num);

//问题1
char name[10]="abcdefg";
Greeting(name);
}

一根烂笔头 2011-06-09
  • 打赏
  • 举报
回复
ouyh12345 2011-06-09
  • 打赏
  • 举报
回复
2
while(num>=0)死循环
ouyh12345 2011-06-09
  • 打赏
  • 举报
回复
strncat(buffer,name,sizeof(buffer)-1);
可能会越界,直接用printf就可以了
  • 打赏
  • 举报
回复
随便挑一个
printf("%s",string); 有可能输出10个a,有可能输出10个a之后是乱码

64,652

社区成员

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

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