写个C和指针习题程序犯太多错误,记下提醒自己
2013-11-18 16:29
302 查看
// c_chapt13_1.cpp : Defines the entry point for the console application. //编写程序,从标准输入读取一些字符,并根据分类计算各类字符所占的百分比 //禁止使用一系列if或者switch语句 #include "stdafx.h" #include <cstdio> #include <cctype> int is_not_print(int ch) { return !isprint(ch); } int _tmain(int argc, _TCHAR* argv[]) { //声明函数指针 int (*classity_char[])(int)={ iscntrl,//判断控制字符 isspace, isdigit, islower, isupper, ispunct, is_not_print }; //声明各种字母名称数组 char *name_of_chars[]={ "control", "whitespace", "digit", "lower case", "upper case", "punctuation", "non printable" }; //声明各种字母数量数组 //int numbers_of_chars=(sizeof(classity_char)/sizeof(classity_char[0])); const int numbers_of_chars=(sizeof(classity_char)/sizeof(classity_char[0])); //#define numbers_of_chars ( sizeof( classity_char )/sizeof( classity_char[0] ) ) static int numbers[ numbers_of_chars ]; int total=0; char ch; //输入字母并判断 while ( (ch=getchar())!=EOF ) { total+=1; for(int i=0;i<numbers_of_chars;++i) { if ( classity_char[i]( ch ) ) numbers[i]+=1; } } //输出结果 if (0==total) { printf("There is no print."); } else { for (int i=0;i<numbers_of_chars;++i) { printf("%f%% %s characters\n",numbers[i]*100.0/total,name_of_chars[i]); } } printf("\n\n\nThank you!!!"); return 0; }
错误1: int is_not_print(int ch) { return !isprint(ch); } 写成 int is_not_print(int) { return !isprint(int); } Error2 error C2660: 'isprint' : function does not take 0 argumentsd:\我的c++\练习\c_chapt13_1\c_chapt13_1\c_chapt13_1.cpp11
错误2:关于定义数组大小 int numbers_of_chars=(sizeof(classity_char)/sizeof(classity_char[0])); 错误:1>d:\我的c++\练习\c_chapt13_1\c_chapt13_1\c_chapt13_1.cpp(45) : error C2057: expected constant expression 1>d:\我的c++\练习\c_chapt13_1\c_chapt13_1\c_chapt13_1.cpp(45) : error C2466: cannot allocate an array of constant size 0 1>d:\我的c++\练习\c_chapt13_1\c_chapt13_1\c_chapt13_1.cpp(45) : error C2133: 'numbers' : unknown size 数组大小定义时应为常量,而代码中给的是变量,顾出现上述错误,可以定义成const。
错误3:宏定义不是语句,不应该加分号。 #define numbers_of_chars ( sizeof( classity_char )/sizeof( classity_char[0] ) ) 虽说知道概念,但是使用时还是出现错误,因为本人很少这样使用,习惯性加了分号,导致任何一个使用该宏的地方都有错误。谨记!!!
错误4:未初始化数组, 编译无错后,运行结果莫名其妙。于是F9设置断点观察numbers数组数据,发现不对劲。才发现为初始化数组。 由于数组元素都初始化为0,我将其设置成static变量,利用编译器对其自动初始化,也行这并不是一个好主意!! 不要在同一个地方犯第二次错误,故记录下~~~
相关文章推荐
- 使用多线程方法实现一个死锁程序,用来提醒自己不要犯这样的错误
- DZM 暴露,记下自己愚蠢的错误
- 微信小程序细节坑(自己刚开始做小程序中遇到的一些细节,如有错误,还请大佬指正,看心情更新。。。。)
- C 语言程序中与存储器相关的常见错误(即指针相关)
- 高手帮我看看这个指向函数的指针的程序错误。。。。。。。谢谢
- 《core python programming 》第二十三章的习题的部分解答,自己做的,错误肯定难免的
- 还是犯了一个优先级的错误_sql,记录下,提醒自己
- 一个VB小程序,提醒自己上下班打卡和每小时休息
- 周末发现一个BUG,时有时无,一出程序就崩溃,郁闷了好久,终于跟出来来了,记之,提醒自己今后一定规范编写,只要规范,绝对不会出问题
- 一个错误的链表程序 ——编译通过,执行出错。自己没有好的调试技术,没有找到错误;而靠经验得逐行细查——费时间
- 这段程序还搞错一个输出(写在这里提醒自己)
- 练习1-1 在你自己的程序中运行“hello,world”程序。再有意去掉部分内容,看看会得到什么错误信息。
- 从一个简单程序的GDB调试看段错误,指针的初始化及内存分配(malloc/free)
- iOS开发中的错误整理,百思项目'我的'模块,tableFooterViewHeight的问题.提醒自己对KVO和Block的运用欠缺
- C 语言程序中与存储器相关的常见错误(即指针相关)
- 自己写的堆排序程序,编写时有几个错误点,见注释,已通过dev c++调试通过。
- 牛客网“程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)”错误的可能原因
- 提醒自己,makefile 里的gcc ,rm ,都是程序,都得加path
- 解决LINK : fatal error LNK1140: 用于程序数据库的模块太多;链接时使用 /PDB:NONE 的错误
- 给自己强化印象:程序崩溃,第一反应就是用了空指针、空对象