#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int demo(void)
{
char *p0 = NULL;
p0 = (char *)malloc(100);
strcpy(p0, "agdsgds");
int i = 0;
char *p[10] = { 0 };
for (i = 0; i < 10; i++)
{
p[i] = malloc(100);
strcpy(p[i], "agdsg");
}
int a[10];
int *q = (int *)malloc(10 * sizeof(int));
int n = 3;
char **buf = (char **)malloc(n * sizeof(char *));
if (buf == NULL)
{
return -1;
}
for (i = 0; i < n; i++)
{
buf[i] = (char *)malloc(30 * sizeof(char));
char str[30];
sprintf(str, "test%d%d", i, i);
strcpy(buf[i], str);
}
for (i = 0; i < n; i++)
{
printf("%s, ", buf[i]);
}
printf("\n");
for (i = 0; i < n; i++)
{
free(buf[i]);
buf[i] = NULL;
}
if (buf != NULL)
{
free(buf);
buf = NULL;
}
return 0;
}
char **getMem(int n)
{
int i = 0;
char **buf = (char **)malloc(n * sizeof(char *));
if (buf == NULL)
{
return NULL;
}
for (i = 0; i < n; i++)
{
buf[i] = (char *)malloc(30 * sizeof(char));
char str[30];
sprintf(str, "test%d%d", i, i);
strcpy(buf[i], str);
}
return buf;
}
void print_buf(char **buf, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%s, ", buf[i]);
}
printf("\n");
}
void free_buf(char **buf, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
free(buf[i]);
buf[i] = NULL;
}
if (buf != NULL)
{
free(buf);
buf = NULL;
}
}
int main(int argc, const char * argv[])
{
char **buf = NULL;
int n = 3;
buf = getMem(n);
if (buf == NULL)
{
printf("getMem err\n");
return -1;
}
print_buf(buf, n);
free_buf(buf, n);
buf = NULL;
return 0;
}
测试结果
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char **getMem(int num)
{
int i = 0;
char **p2 = NULL;
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 printMyArray(char **myArray, int num)
{
int i = 0;
if (myArray == NULL)
{
return ;
}
for (i=0; i<num; i++)
{
printf("%s \n", *(myArray+i) );
}
}
#if 0
void sortMyArray(char **myArray, int num)
{
int i =0 , j = 0;
if (myArray == NULL)
{
return ;
}
char *tmp = NULL;
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;
}
}
}
}
#else
void sortMyArray(char **myArray, int num)
{
int i =0 , j = 0;
if (myArray == NULL)
{
return ;
}
char tmp[100] = { 0 };
memset(tmp, 0, sizeof(tmp));
for (i=0; i<num; i++)
{
for (j=i; j<num; j++)
{
if (strcmp(myArray[i], myArray[j]) < 0 )
{
strcpy(tmp, myArray[i]);
strcpy(myArray[i], myArray[j]);
strcpy(myArray[j], tmp);
}
}
}
}
#endif
void getMem_Free(char **p2, int num)
{
int i = 0;
for(i=0; i<num; i++)
{
if (p2[i] != NULL)
{
free(p2[i]);
p2[i] = NULL;
}
}
if (p2!=NULL)
{
free(p2);
}
}
int main(int argc, const char * argv [])
{
int i = 0, j = 0;
char **p2 = NULL;
int num = 5;
char *tmp = NULL;
char tmpbuf[100];
p2 = getMem(num);
if (p2 == NULL)
{
return -1;
}
printf("before sort:\n");
printMyArray(p2, num);
sortMyArray(p2, num);
printf("after sort:\n");
printMyArray(p2, num);
getMem_Free(p2, num);
if(p2 != NULL)
{
printf("pointer p2 ok!\n");
p2 = NULL;
}
return 0;
}
测试结果