自定义内存,使用堆中分配内存来控制数据交换
交换指针指向的内容
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char **p2 = NULL; int i,j; int num = 5; p2 = (char **)malloc(sizeof(char *) * num); for(i = 0; i < num; i++) { p2[i] = (char *)malloc(sizeof(char) * 100); sprintf(p2[i],"%d%d%d",i+1,i+1,i+1); } //print printf("before sort\n"); for(i = 0; i < num; i++) { printf("%s \n",p2[i]); } char *tmp = NULL; //sort for(i = 0; i < num; i++) { for(j = i+1; j < num; j++) { if(strcmp(p2[i],p2[j]) < 0) { tmp = p2[i]; p2[i] = p2[j]; p2[j] = tmp; } } } printf("after sort\n"); for(i = 0; i < num; i++) { printf("%s \n",p2[i]); } //free for(i = 0; i < num; i++) { if(p2[i] != NULL) { free(p2[i]); p2[i] = NULL; } } if(p2 != NULL) { free(p2); } return 0; }
交换指针指向的内存块
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char **p2 = NULL; int i,j; int num = 5; char tmpbuf[100]; p2 = (char **)malloc(sizeof(char *) * num); for(i = 0; i < num; i++) { p2[i] = (char *)malloc(sizeof(char) * 100); sprintf(p2[i],"%d%d%d",i+1,i+1,i+1); } //print printf("before sort\n"); for(i = 0; i < num; i++) { printf("%s \n",p2[i]); } char *tmp = NULL; //sort change the storage for(i = 0; i < num; i++) { for(j = i+1; j < num; j++) { if(strcmp(p2[i],p2[j]) < 0) { strcpy(tmpbuf,p2[i]); strcpy(p2[i],p2[j]); strcpy(p2[j],tmpbuf); } } } printf("after sort\n"); for(i = 0; i < num; i++) { printf("%s \n",p2[i]); } //free for(i = 0; i < num; i++) { if(p2[i] != NULL) { free(p2[i]); p2[i] = NULL; } } if(p2 != NULL) { free(p2); } return 0; }
函数封装接口
#include<stdio.h> #include<string.h> #include<stdlib.h> char **getMem(int num) { char **p2 = NULL; int i; p2 = (char **)malloc(sizeof(char *) * num); if(p2 == NULL) { return NULL; } for(i = 0; i < num; i++) { p2[i] = (char *)malloc(sizeof(char) * 100); sprintf(p2[i],"%d%d%d",i+1,i+1,i+1); } return p2; } void sortMyarray(char **myArray,int num) { int i = 0,j = 0; char *tmp = NULL; //sort change the storage for(i = 0; i < num; i++) { for(j = i; j < num; j++) { if(strcmp(myArray[i],myArray[j]) < 0) { tmp = myArray[i]; myArray[i] = myArray[j]; myArray[j] = tmp; } } } } void printMyArray(char **myArray,int num) { int i = 0; for(i = 0; i < num; i++) { printf("%s \n",*(myArray+i)); } } void getMem_free(char **p2,int num) { int i; for(i = 0; i < num; i++) { if(p2[i] != NULL) { free(p2[i]); p2[i] = NULL; } } if(p2 != NULL) { free(p2); } } int main() { char **p2 = NULL; int i,j; int num = 5; char tmpbuf[100]; p2 = getMem(num); //print printf("before sort\n"); printMyArray(p2,num); sortMyarray(p2,num); printf("after sort\n"); printMyArray(p2,num); //free getMem_free(p2,num); return 0; }
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char
**p2 = NULL;
int
i,j;
int
num = 5;
p2
= (char **)malloc(sizeof(char *) * num);
for(i
= 0; i < num; i++)
{
p2[i]
= (char *)malloc(sizeof(char) * 100);
sprintf(p2[i],"%d%d%d",i+1,i+1,i+1);
}
printf("before
sort\n");
for(i
= 0; i < num; i++)
{
printf("%s
\n",p2[i]);
}
char
*tmp = NULL;
//sort
for(i
= 0; i < num; i++)
{
for(j
= i+1; j < num; j++)
{
if(strcmp(p2[i],p2[j])
< 0)
{
tmp
= p2[i];
p2[i] = p2[j];
p2[j]
= tmp;
}
}
}
printf("after
sort\n");
for(i
= 0; i < num; i++)
{
printf("%s
\n",p2[i]);
}
//free
for(i
= 0; i < num; i++)
{
if(p2[i]
!= NULL)
{
free(p2[i]);
p2[i]
= NULL;
}
}
if(p2
!= NULL)
{
free(p2);
}
return
0;
}
原文地址:https://www.cnblogs.com/wanghao-boke/p/11625376.html