1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 二级指针的第三种内存模型 */ //打印字符串数组 int printArray( char **arrayStr, int num)
{ int i = 0;
for (i=0; i<num; i++)
{
printf ( "%s \n" ,arrayStr[i]);
}
return 0;
} //排序 int sortArrayStr( char **arrayStr, int num)
{ int i = 0, j = 0;
char *temp = NULL;
for (i=0; i<num; i++)
{
for (j=i+1; j<num; j++)
{
if ( strcmp (arrayStr[i],arrayStr[j]) < 0)
{
temp = arrayStr[i];
arrayStr[i] = arrayStr[j];
arrayStr[j] = temp;
}
}
}
return 0;
} //二级指针的第三种内存模型 int main()
{ int i = 0, j = 0;
char buf[100];
char **arrayStr = ( char **) malloc (10* sizeof ( char *)); //int array[10]
if (arrayStr == NULL)
{
return ;
}
for (i=0; i<10; i++)
{
arrayStr[i] = ( char *) malloc (100* sizeof ( char )); // char buff[100]
if (arrayStr[i] == NULL)
{
printf ( "ddde\n" );
return ;
}
sprintf (arrayStr[i], "%d%d%d" ,i,i,i);
}
printf ( "排序之前\n" );
printArray(arrayStr,10);
sortArrayStr(arrayStr,10);
printf ( "排序之后\n" );
printArray(arrayStr,10);
for (i=0; i<10; i++)
{
free (arrayStr[i]);
}
if (arrayStr != NULL)
{
free (arrayStr);
}
system ( "pause" );
return 0;
}
|
本文转自arac 51CTO博客,原文链接:http://blog.51cto.com/skyarac/1598783,如需转载请自行联系原作者