From cd812d5d38c179818d0f5888dd5c598b1c9c1b44 Mon Sep 17 00:00:00 2001 From: ViolentAyang <76544389+ViolentAyang@users.noreply.github.com> Date: Fri, 1 Apr 2022 10:47:24 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E4=B8=B2=E7=9A=84=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=AE=9E=E7=8E=B0.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 串/串的顺序存储实现.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 串/串的顺序存储实现.c diff --git a/串/串的顺序存储实现.c b/串/串的顺序存储实现.c new file mode 100644 index 0000000..06aeee2 --- /dev/null +++ b/串/串的顺序存储实现.c @@ -0,0 +1,26 @@ +#include +#include +#include +#define MAXLEN 255 //预定义最大串长为255 + +//串的顺序存储 静态数组实现(定长顺序实现) +typedef struct{ + char ch[MAXLEN]; //每个分量存储一个字符 分配连续的存储空间,每个char字符占1B + int length; //串的实际长度 +}SString; +//动态数组实现(堆分配存储) +typedef struct{ + char *ch; //按串长分配存储区,ch指向串的基地址 + int length; //串的长度 +}HString; + +int main(){ + HString S; + S.ch = (char *)malloc(sizeof(char)); //用完需要手动free + return 0; +} +//四种方案 +//第一种方法 定义一个字符串数组 加上一个记录长度的length变量 +//第二种方法 ch[0]充当length 优点:字符串的位序和数组下标相同 缺点:只能表示0-255的长度 +//第三种方法 没有length变量 以字符'\0'表示结尾(对应ASCII码中的0) 缺点:想知道长度需要遍历 +//第四种方法 舍弃第一个位置 加上一个length变量