词法分析之动态字符串
来源:互联网 发布:淘宝有销量没评价 编辑:程序博客网 时间:2024/05/17 06:52
C语言字符串并没有记录系统为其分配的长度,用户必须自己将字符串长度保存在其他变量中,操作不当会产生错误,导致缓存区溢出,于是决定自己定义一个动态字符串数据结构
typedef struct DynString{ int count; //字符串长度 int capacity; //字符串容量 char *data; //指向字符串的指针}DynString/**总共定义了5个函数,动态字符串的初始化,清空,重置,重新分配容量大小,追加字符**/void dynstring_init(DynString *pstr,int initsize)/**初始化,initsize表示分配空间大小**/{ if(pstr!=NULL) { pstr->data=(char *)malloc(sizeof(char)*initsize); pstr->count=0; pstr->capacity=initsize; }}void dynstring_free(DynString *pstr)/**清空**/{ if(pstr!=NULL) { if(pstr->data) free(pstr->data); pstr->count=0; pstr->capacity=0; }}void dynstring_reset(DynString *pstr)/**重置,先把字符串清空,再初始化分配8个字符空间大小**/{ dynstring_free(pstr); dynstring_init(pstr,8);}void dynstring_realloc(DynString *pstr,int new_size)/**重新分配容量大小**/{ int capacity; char *data; capacity=pstr->capacity; while(capacity<new_size) { capacity*=2; } data=(char *)realloc(pstr->data,capacity); if(!data) error("内存分配失败");/**这个函数需要自己定义,意思是出现错误,并输出里面的字符串,暂时不用管**/ pstr->data=data; pstr->capacity=capacity;}void dynstring_chcat(DynString *pstr,int ch)/**追加字符ch**/{ int count; count=pstr->count+1; if(count>pstr->capacity) dynstring_realloc(pstr,count); ((char *)pstr->data)[count-1]=ch; pstr->count=count;}
0 0
- 词法分析之动态字符串
- 词法分析(字符串分析)
- 词法分析(字符串分析)
- javascript之词法分析
- (转)词法分析(字符串分析)
- 编译原理 之 词法分析
- 编译原理之词法分析
- 编译原理之词法分析
- js笔记之词法分析
- 编译原理之词法分析
- 编译原理之词法分析
- 编译器之词法分析c
- 编译原理之词法分析
- 编译原理之词法分析
- 词法分析之浮点数分析
- C#词法分析器之词法分析的使用详解(一)
- 词法分析之LED文件批量生成
- LUA源码阅读之词法分析
- BestCoder Round #88
- 编辑helloword
- 指南针demo
- hihocoder 118周 网络流四·最小路径覆盖(二分匹配,好题)
- 操作系统(一)概述
- 词法分析之动态字符串
- 2-SAT (Tarjan)—— Katu Puzzle ( POJ 3678 )
- 26. Remove Duplicates from Sorted Array
- strtok 分割字符串
- Java并发编程:线程池的使用
- 189. Rotate Array
- 80端口监听htdos,网站根目录为其子目录
- 数组和指针的区别
- ZigZag Conversion