字符串最易犯的错误 总是和 那个\0 有关
下面程序有一处错误导致 多错误:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <assert.h> 5 typedef struct{ 6 int l, c; // lines, cols 7 } pos_t; 8 9 typedef struct { 10 char *str; 11 pos_t pos; 12 int w, h; // the width and height of a recttangle 13 void(*set)(void *); // the way of display 14 } str_obj; 15 16 char *buf; 17 int L, C; 18 19 void init(int line, int cols); 20 void uninit(void); 21 void display(void); 22 void set_pixel(int cols, int line, char ch); 23 static void hor_line(int c, int l, int length); 24 static void ver_line(int c, int l, int length); 25 void print(void *obj); 26 27 int main(int argc, char **argv) 28 { 29 30 init(23, 80); 31 32 str_obj xstr = { 33 "The UNIX operating system provides its services through " 34 "a set of system calls, which are in effect functions wit" 35 "hin the operating system that may be called by user prog" 36 "rams. This chapter describes how to use some of the most" 37 " important system calls from C programs.", 38 {2,5}, 15,20, print 39 }; 40 41 xstr.set(&xstr); 42 xstr.pos.c += xstr.w+3; 43 xstr.set(&xstr); 44 45 str_obj ystr = { 46 "The only way to learn a new programming language is by " 47 "writing programs in it.", 48 {5, 50}, 20, 5, print 49 }; 50 ystr.set(&ystr); 51 ystr.pos.l += ystr.h+1; 52 ystr.set(&ystr); 53 54 display(); 55 56 uninit(); 57 return 0; 58 } 59 60 void init(int line, int cols) 61 { 62 if(line < 1 || cols < 1){ 63 printf("init: parameter error."); 64 exit(1); 65 } 66 L = line, C = cols; 67 buf = malloc(L*C); 68 memset(buf, ' ', L*C); 69 } 70 71 void uninit(void) 72 { 73 free(buf); 74 } 75 void display(void) 76 { 77 buf[L*C] = 0; 78 printf("%s", buf); 79 } 80 void set_pixel(int cols, int line, char ch) 81 { 82 if(line > 0 && line <= L && cols > 0 && cols <= C) 83 buf[(line-1)*C + cols-1] = ch; 84 } 85 86 static void hor_line(int c, int l, int length) 87 { 88 set_pixel(c, l, '+'); 89 int i; 90 for(i=c+1; i<length+c-1; i++) 91 set_pixel(i, l, '-'); 92 set_pixel(i,l, '+'); 93 } 94 95 static void ver_line(int c, int l, int length) 96 { 97 set_pixel(c, l, '+'); 98 int i; 99 for(i=l+1; i<length+l-1; i++) 100 set_pixel(c, i, '|'); 101 set_pixel(c, i, '+'); 102 } 103 104 void print(void *obj) 105 { 106 str_obj *p = obj; 107 char *str = p->str; 108 int x = p->pos.c, y = p->pos.l; 109 int w = p->w, h = p->h; 110 111 hor_line(x-2, y-1, w+4); 112 ver_line(x-2, y-1, h+2); 113 hor_line(x-2, y+h, w+4); 114 ver_line(x+w+1, y-1, h+2); 115 116 int i,j; 117 int len = strlen(str), count = 0; 118 for(i=0; i<h; i++){ 119 for(j=0; j<w; j++){ 120 if(count < len ){ 121 set_pixel(j+x,y+i, str[count++]); 122 } 123 } 124 } 125 }
给字符串指针分配 堆空间时一定要 记得 算上后面的\0;
这么晚了,算是教训,睡觉;
转载于:https://www.cnblogs.com/mathzzz/archive/2012/07/16/2592919.html
字符串最易犯的错误 总是和 那个\0 有关相关推荐
- javascript中易犯的错误有哪些
javascript中易犯的错误有哪些 一.总结 一句话总结: 比如循环中函数的使用 函数中this的指向谁(函数中this的使用) 变量的作用域 1.this.timer = setTimeout( ...
- Android4.0 Design之UI设计易犯的错误2
想成为Android的杰出开发工程师,不懂得Android的设计规则怎么可以,Android4.0问世后谷歌公司为Android程序员规范了一系列的设计原则,不要再盲目的模仿IOS的设计了,因为And ...
- css html 对错号,HTML_DIV+CSS编码时易犯的错误,CSS+DIV是网站标准(或称“WEB - phpStudy...
DIV+CSS编码时易犯的错误 CSS+DIV是网站标准(或称"WEB标准")中常用的术语之一,通常为了说明与HTML网页设计语言中的表格(table)定位方式的区别,因为XHTM ...
- 7个跑步易犯的错误和解决办法
似乎所有人都认为跑步是一种非常简单的锻炼方式,然而,其实不然,跑步涉及到许多专业知识.错误的跑步,不仅影响锻炼效果,而且还容易导致受伤. 1.鞋子不合适 问题:穿着太旧的跑步鞋或者类型不合适的运动鞋容 ...
- Linux管理员易犯的错误
对于初入linux的管理员们来说,迁移到Linux是一场噩梦,而且在Linux管理中稍微不小心就会出错,如果不避免这些错误的话就会给我们的网络和系统带来风险,那么我们现在就去看看Linux管理员易犯的 ...
- 外汇资金管理上易犯的错误
赚钱是做外汇投资的最终目的,为了获取更多的钱,首先管理好自己手中的钱是每位投资者走向成功的必由之路,能够正确冷静科学合理的运用资金,很大程度上区分了一位投资者是否优秀.一般而言,在外汇资金管理方面投资 ...
- 5个golang中易犯的错误
点击上方蓝字关注我们 To err is human,to forgive divine. -Alexander Pope 初学golang我们经常会犯一些错误,虽然它们不会产生类型检查的异常,但是它 ...
- Linux初到者五个最易犯的错误
随着Ubuntu 10.10的到来,为你的业务试用Linux的理由列表又变长了一些.此款自由与免费的操作系统现在变得空前用户友好,而又提供了其竞争对手所不具备的安 全性等其他优势.如果你是新Linux ...
- Android4.0 Design之UI设计易犯的错误1
想成为Android的杰出开发工程师,不懂得Android的设计规则怎么可以,Android4.0问世后谷歌公司为Android程序员规范了一系列的设计原则,不要再盲目的模仿IOS的设计了,因为And ...
最新文章
- android studio gradle 位置更改
- 1小时教你做360度全景“小星球”效果图 Skillshare – Create a Panoramic ‘Little Planet’ from Anywhere
- Lync 小技巧-42-动态-IP-统一沟通-环境-IP-变更后-操作
- Redis 基本操作一
- 3算法全称_全网最通俗的KMP算法图解
- 剑指 Offer 35. 复杂链表的复制(哈希/衍生拆分图解)
- ORB-SLAM3单目初始化,地图的初始化
- 移动滑块改变使用容量
- CityEngine 2014安装教程
- 重装助手教你如何在Windows中正确调整屏幕分辨率设置
- macbook重装系统 选择方案_Mac重装系统教程(二):网络在线重装系统
- n个人有c个魔法帽几天去掉所有的帽子
- 腾讯云PCDN:从P2P到万物互联服务框架
- 朋友圈那个随便辞职的年轻人,后来活成了什么样?
- 知乎客户端埋点流程、模型和平台技术
- 韩泰机器人_韩泰轮胎怎么样?进击的韩泰,拥有着对未来无限的可能!
- Gephi实战教程:从入门到精通
- Validation框架的应用
- java print 格式化输出_java 格式化输出方法
- HTML表格自动排序