#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//把p1和p2中的字符串拼接到p3中
//p3每行的长度不固定
int myStrCat(char* p1[],int num1,char p2[10][30],int num2,char*** p3/*out*/,int* num3){
char** pTmp = NULL;//辅助指针
pTmp = (char**)malloc(sizeof(char*)*(num1+num2));
int row_len=0;
//p1数据copy到p3
for(int i=0;i<num1;i++){
row_len = strlen(p1[i]);
pTmp[i] = (char*)malloc(sizeof(char)*(row_len+1));//注意多分配一字节存'\0',否则释放内存出错
strcpy(pTmp[i],p1[i]);
}
//p2数据copy到p3
for(int i=0;i<num2;i++){
row_len = strlen(p2[i]);
pTmp[i+num1] = (char*)malloc(sizeof(char)*(row_len+1));//注意多分配一字节存'\0',否则释放内存出错
strcpy(pTmp[i+num1],p2[i]);
}
*num3 = num1+num2;
*p3 = pTmp;//挂上
return 0;
}
int freeMyStrCat(char*** p,int row){//释放二维数组
char** tmp = *p;
for(int i=0;i<row;i++){
if (tmp[i]!=NULL
二维数组的三种内存模型
最新推荐文章于 2022-05-24 17:31:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)