C/C++ 题
来源:互联网 发布:电力系统计算软件 编辑:程序博客网 时间:2024/05/15 13:06
1、请找出下面程序中的BUG,并说明原因
#include "stdio.h"
void ToUpCase(char * pStr)
{
int len = (int)strlen(pStr);
int i = 0;
for (i = 0; i < len; i++ )
{
if ((pStr[i] >= 'a') && (pStr[i] <= 'z'))
{
pStr[i] -= 32;
}
}
return;
}
int main(int argc, char * argv[])
{
char *testString = "abcd";
ToUpCase(testString);
printf("%s\r\n", testString);
return;
}
参考答案:
char *testString = "abcd";是错误的,这样可以使用testString来修改字符常量"abcd",这样会导致改写只读数据异常。
应该使用:const char * testString,而且在后续的使用中,都不能强制转化成char *。
2、请找出下面程序中的BUG,并说明原因
#include <stdio.h>
int main(int argc, char * argv[])
{
char mVar = -1;
if (mVar == -1)
{
printf("I am OK.\r\n");
}
else
{
printf("I am error.\r\n");
}
return 0;
}
参考答案:
标准C中,char类型,signed char,unsighed char是三个不同的类型,给char类型赋值为-1时,不同的编译器解释不一样,有的是255,有的就是-1;
正确的做法是:针对char类型,最好只用于字符场景,而不要当数字使用。
- c++/c笔试题
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- sharepoint2010 sharepoint管理中心连接到fast失败处理
- 理解装配中的文件引用-1
- Eclipse插件安装之link方式
- DHTMLX Tree中文开发指导
- 弹出键盘的怪事
- C/C++ 题
- Qt for Android 使用 手记
- linux----文件系统中相关数据结构的关系
- 计算biSizeImage的两种方法(转)
- Keycode对照表
- JAVA缓存机制
- ubuntu 修改shell分辨率(适用于桌面和Server版本),非menulist方法
- 兼容浏览器的min-height和min-width
- 窗宽窗位与其处理方法