字符串最易犯的错误 总是和 那个\0 有关

下面程序有一处错误导致 多错误:

View Code
  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/p/2592919.html