答案仅供参考一、改错题1
找出以下两处逻辑错误(非编程法律规范)并改正 char* getErrorString(int errcode){char errorString[] = "not exist"; char errorString2[] = "not available";switch(errcode) { case 1: return errorString; case 2: return errorString2; default: return NULL;}}void printErrorString(int errcode){char *errorString = getErrorString(errcode);printf("errcode:%d, errorString:%s\n", errcode, errorString); }case1 和 case2 返回了两个局部变量指针,而局部变量在函数结束后将自动释放
改正:将字符数组改为指针 char *errorString= "not exist"; char *errorString2 = "not available";2
以下是输出“welcome home”的程序,找出逻辑错误(非编程法律规范)并改正 const int MAX_STR_SIZE = 12; int main(int argc, char *argv[]) { char str[MAX_STR_SIZE]; strcpy(str,"welcome home"); printf("%s", str);return 0; } “welcome home”字符串应该是 13 个字节,而 str 字符数组只能存储 12 个字符 改正: const int MAX_STR_SIZE = 13;二、程序填空题1
写出 String