string.h
1 #pragma once 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <locale.h> 5 #include <string.h> 6 #include <memory.h> 7 8 9 //stringA类型 10 struct stringA 11 { 12 char *p; 13 int memlength;//内存长度 14 }; 15 16 //stringW类型 17 struct stringW 18 { 19 wchar_t *p; 20 int memlength; 21 }; 22 23 //set_locale 24 void setbackground(const char *str); 25 //初始化,p分配是stringA类型还是stringW类型 26 int init(void *p, char ch);//ch = w ch = a自定义 27 //初始化数据 28 void initwithstring(void *p, char ch, void *pstr); 29 //显示数据 30 void show(void *p, char ch);
string.cpp
1 #include "string.h" 2 3 //初始化数据 4 int init(void *p, char ch)//ch = w ch = a自定义 5 { 6 if (ch == ‘w‘) 7 { 8 struct stringW *pw = (struct stringW *)p;//指针类型转换 9 pw->p = NULL;//指向为空 10 pw->memlength = 0;//长度为0 11 } 12 else if (ch == ‘a‘) 13 { 14 struct stringA *pa = (struct stringA *)p;//指针类型转换 15 pa->p = NULL;//指向为空 16 pa->memlength = 0;//长度为0 17 } 18 else 19 { 20 abort(); 21 } 22 } 23 24 //用字符串初始化 25 void initwithstring(void *p, char ch, void *pstr) 26 { 27 if (ch == ‘w‘) 28 { 29 struct stringW *pw = (struct stringW *)p;//指针类型 30 pw->p = NULL; 31 pw->memlength = 0; 32 33 wchar_t *pstrbak = (wchar_t *)pstr;//转换宽字符 34 pw->p = (wchar_t *)malloc((wcslen(pstrbak) +1)* 2); 35 pw->memlength = wcslen(pstrbak) + 2; 36 wcscpy(pw->p, pstrbak);//拷贝 37 } 38 else if (ch == ‘a‘) 39 { 40 struct stringA *pa = (struct stringA *)p; 41 pa->p = NULL; 42 pa->memlength = 0; 43 44 char *pstrbak = pstr;//转换窄字符 45 pa->p = malloc(strlen(pstrbak) + 1);//分配内存, 46 pa->memlength = strlen(pstrbak) + 1;//内存长度 47 strcpy(pa->p, pstrbak); 48 } 49 } 50 51 //显示 52 void show(void *p, char ch) 53 { 54 55 if (ch == ‘w‘) 56 { 57 struct stringW *pw = p;//指针类型转换,确定类型 58 if (pw->p != NULL) 59 { 60 wprintf(L"%ls\n", pw->p); 61 } 62 } 63 else if (ch == ‘a‘) 64 { 65 struct stringA *pa = p;//指针类型转换,确定类型 66 if (pa->p != NULL) 67 { 68 printf("%s\n", pa->p); 69 } 70 } 71 } 72 73 void setbackground(const char *str) 74 { 75 setlocale(LC_ALL, str); 76 }
array.h
1 #include "string.h" 2 3 //字符串结构体 4 struct dataArray 5 { 6 //void类型的指针,用的时候再进行类型转换 7 void *p; 8 //字符串数组长度 9 int length; 10 //数据类型 stringa stringw 11 char datatype[10]; 12 //每一个元素的大小 sizeof(stringa) sizeof(stringw) 13 int elemsize; 14 }; 15 16 //初始化 结构体 数组类型 数组每一个元素大小 17 void initWithNull(struct dataArray *pDataArray, char datatype[10], int elemsize); 18 19 // 结构体 数组类型 每一个元素大小 数组首地址 长度 20 void initWithArray(struct dataArray *pDataArray, char datatype[10], int elemsize, void *parray, int n); 21 22 //在数组末尾添加一个元素 23 void backAddOne(struct dataArray *pdataarray, void *parray);//传递一个元素的地址 24 25 //显示数组 26 void showarray(struct dataarray *pdataarray, char datatype[10]);
array.cpp
1 #pragma once 2 3 #include "array.h" 4 5 //初始化 6 void initWithNull(struct dataArray *pDataArray, char datatype[10], int elemsize) 7 { 8 //指针赋值为NULL 9 pDataArray->p = NULL; 10 //长度赋为0 11 pDataArray->length = 0; 12 //每一个元素的大小 13 pDataArray->elemsize = elemsize; 14 //把类型传给数组中的datatype 15 strcpy(pDataArray->datatype, datatype); 16 } 17 18 //用一个array数组进行初始化 19 void initWithArray(struct dataArray *pDataArray, char datatype[10], int elemsize, void *parray, int n) 20 { 21 //总指针赋值为NULL 22 pDataArray->p = NULL; 23 //数组长度赋为0 24 pDataArray->length = 0; 25 //每一个元素的大小 26 pDataArray->elemsize = elemsize; 27 //把类型传给数组中的datatype 28 strcpy(pDataArray->datatype, datatype); 29 30 //如果是"stringa"类型 31 if (strcmp(pDataArray->datatype, "stringa") == 0) 32 { 33 //指针类型转换 34 struct stringA *pa = parray; 35 //每一个元素的大小 36 pDataArray->elemsize = sizeof(struct stringA); 37 //分配内存 38 pDataArray->p = malloc(sizeof(struct stringA)*n); 39 //内存拷贝,把所有结构体拷贝过去 40 memcpy(pDataArray->p, parray, sizeof(struct stringA)*n); 41 //长度为n 42 pDataArray->length = n; 43 } 44 } 45 46 //在数组尾部添加一个元素 47 void backAddOne(struct dataArray *pdataarray, void *parray) 48 { 49 //如果是"stringa"类型 50 if (strcmp(pdataarray->datatype, "stringa") == 0) 51 { 52 pdataarray->p = realloc(pdataarray->p, sizeof(struct stringA)*(pdataarray->length + 1));//拓展数据长度 53 //接受首地址 54 struct stringA *pa = pdataarray->p; 55 struct stringA *palast = parray;//转换类型 56 57 pa[pdataarray->length].p = palast->p; 58 pa[pdataarray->length].memlength = palast->memlength; 59 60 pdataarray->length += 1; 61 } 62 } 63 64 //显示数据 65 void showarray(struct dataArray *pdataarray, char datatype[10]) 66 { 67 //如果是"stringa"类型 68 if (strcmp(pdataarray->datatype, "stringa") == 0) 69 { 70 //转换数据类型 71 struct stringA *pa = pdataarray->p; 72 //依次循环,输出数据 73 for (int i = 0; i < pdataarray->length; i++) 74 { 75 printf("%s ", pa[i].p);//打印字符串 76 } 77 printf("\n"); 78 } 79 }
main.c
1 #include "string.h" 2 #include "array.h" 3 4 void main() 5 { 6 //setbackground("zh-CN"); 7 //struct stringA stringa1; 8 //struct stringW stringw1; 9 ////init(&stringa1, ‘a‘); 10 //initwithstring(&stringa1, ‘a‘, "calc"); 11 //initwithstring(&stringw1, ‘w‘, L"notepad78你好"); 12 //show(&stringa1, ‘a‘); 13 //show(&stringw1, ‘w‘); 14 15 struct stringA stringa[6]; 16 char *str[6] = { "calc","notepad","pause","12345","test" ,"hello"}; 17 18 for (int i = 0; i < 6; i++) 19 { 20 initwithstring(&stringa[i], ‘a‘, str[i]); 21 } 22 23 struct dataArray data; 24 initWithArray(&data, "stringa", sizeof(struct stringA), stringa, 5); 25 showarray(&data, "stringa"); 26 27 //尾部插入 28 backAddOne(&data, &stringa[2]); 29 showarray(&data, "stringa"); 30 31 32 system("pause"); 33 }
运行截图:
总结:数组库有一个void类型的指针,可以指向很多数组,并控制数组.数组传递的是void类型,根据后面的参数进行转换,并分配内存.
原文地址:https://www.cnblogs.com/xiaochi/p/8379695.html
时间: 2024-10-09 09:47:25