lineholder.c

来自「String Pointers to explore how the point」· C语言 代码 · 共 55 行

C
55
字号
/* lineholder.c- Bob Wilson  */#include <stdio.h>#include <string.h>#include "lineholder.h"void *malloc(int);void free(void*);static char *lines[MAXLINE];static int first, last, end, wrapped;void init_lineholder(int nlines){        if (nlines > MAXLINE)                nlines = MAXLINE;	first = last = 0;	end = nlines-1;	wrapped = 0;}void insert_line(char *line){	if (lines[last] != NULL)		free((void *) lines[last]);	if ((lines[last] =  (char * ) malloc(strlen(line) + 1)) != NULL)	   strcpy(lines[last], line);        last++;          if (last > end) {                last = 0;                   wrapped = 1;        }	if (wrapped)		first = (++first > end)? 0 : first;}void print_lines(void){        int i;        for (i = 0; i <= end; i++) {                if (lines[first] != NULL) {                        printf("%s", lines[first]);                        free ((void *) lines[first]);                        lines[first] = NULL;                }                first = (++first > end)? 0 : first;        }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?