1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
int
main(
void
)
{
int
a = 10;
//声明一个变量a
int
*p = &a;
//声明指针p,指向变量a
int
**q = &p;
//声明二级指针q,指向一级指针p
printf
(
"a = %d\n"
,a);
//打印变量a的值
printf
(
"a的地址&a=%p\n"
,&a);
//打印变量a的地址
printf
(
"p = %p\n"
,p);
//打印p的值
printf
(
"p的地址&p=%p\n"
,&p);
//打印p的地址
printf
(
"p的解引用*p=%d\n"
,*p);
//打印p的解引用
printf
(
"q = %p\n"
,q);
//打印q的值
printf
(
"q的地址&q=%p\n"
,&q);
//打印q的地址
printf
(
"q的解引用*q=%p\n"
,*q);
//打印q的解引用
printf
(
"q的双重解引用**q=%d\n"
,**q);
//打印q的双重解引用
return
0;
}
|
1
2
3
4
5
6
|
int
main(
void
)
{
int
arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int
**p = arr;
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int
main(
void
)
{
int
arr[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//分别使用不同的形式打印第2行,第3个元素的值和地址
//根据推到出来的规律如下
//该元素的地址
printf
(
"%p\n"
,*(arr+2*4)+3);
printf
(
"%p\n"
,arr[2]+3);
printf
(
"%p\n"
,&arr[2][3]);
//该元素的值是12
printf
(
"%d\n"
,*(*(arr+2)+3));
printf
(
"%d\n"
,arr[2][3]);
return
0;
}
|
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
|
//打印数组
void
print_Array(
char
(*p)[30],
int
*len)
{
for
(
int
i=0;i<*len;i++)
{
printf
(
"%s\n"
,p[i]);
}
}
//数组排序
int
sort(
char
(*p)[30],
int
*len)
{
int
ret = 0;
char
tmp[100];
if
(p==NULL||len==NULL)
{
printf
(
"function sort error"
);
ret = -1;
}
for
(
int
i=0;i<*len;i++)
{
for
(
int
j=i+1;j<*len;j++)
{
if
(
strcmp
(p[i],p[j])>0)
{
strcpy
(tmp,p[i]);
strcpy
(p[i],p[j]);
strcpy
(p[j],tmp);
}
}
}
return
ret;
}
int
main(
void
)
{
char
array[10][30] = {
"sdecs"
,
"codeq"
,
"owxja"
,
"qwer"
,
"bsdws"
};
int
count = 5;
print_Array(array, &count);
sort(array, &count);
printf
(
"排序之后的数组\n"
);
print_Array(array, &count);
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
sdecs
codeq
owxja
qwer
bsdws
排序之后的数组
bsdws
codeq
owxja
qwer
sdecs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
int
main(
void
)
{
int
a = 1;
int
b = 2;
int
c = 3;
int
*d = &a;
int
*e = &b;
int
*f = &c;
int
*p[3] = { d, e, f };
printf
(
"%d\n"
,*p[0]);
printf
(
"%d\n"
, *d);
printf
(
"%p\n"
, p[0]);
printf
(
"%p\n"
, &a);
return
0;
}
|
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
|
//数组打印
void
print_array(
const
char
**p,
const
int
*len)
{
for
(
int
i=0;i<*len;i++)
{
printf
(
"%s\n"
,p[i]);
}
}
//元素排序
int
sort(
char
**p,
int
*len)
{
char
**str = p;
int
*count = len;
char
*tmp = NULL;
//函数返回值
int
ret = 0;
if
(str==NULL||len==NULL)
{
printf
(
"function sort error"
);
return
ret;
}
for
(
int
i=0;i<*count;i++)
{
for
(
int
j=i+1;j<*count;j++)
{
if
(
strcmp
(str[i],str[j])>0)
{
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
}
return
ret;
}
int
main(
void
)
{
char
*array[] = {
"sdjd"
,
"asdjf"
,
"peroa"
,
"asoeq"
};
int
count =
sizeof
(array)/
sizeof
(array[0]);
//排序前打印
print_array(array, &count);
//调用排序函数
sort(array, &count);
//排序后打印
printf
(
"排序后打印\n"
);
print_array(array, &count);
return
0;
}
|
1
2
3
4
5
6
7
8
9
|
sdjd
asdjf
peroa
asoeq
排序后打印
asdjf
asoeq
peroa
sdjd
|
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
79
80
81
|
//排序
int
sort(
char
***p,
int
*len)
{
char
*tmp = NULL;
int
ret = 0;
if
(p==NULL||len==NULL)
{
printf
(
"function sort error"
);
ret = -1;
}
for
(
int
i=0;i<*len;i++)
{
for
(
int
j=i+1;j<*len;j++)
{
if
((*p)[i]<(*p)[j])
{
tmp = (*p)[i];
(*p)[i] = (*p)[j];
(*p)[j] = tmp;
}
}
}
return
ret;
}
//给二级指针分配内存
int
getMem(
char
***p,
int
count)
{
int
ret = 0;
if
(p==NULL)
{
printf
(
"function getMem error"
);
ret = -1;
}
*p = (
char
**)
malloc
(
sizeof
(
char
*)*(count));
for
(
int
i=0;i<count;i++)
{
(*p)[i] = (
char
*)
malloc
(
sizeof
(
char
)*100);
sprintf
((*p)[i],
"%d%d%d"
,i,i,i);
}
return
ret;
}
//打印数组
void
printArray(
char
***p,
int
count)
{
for
(
int
i=0;i<count;i++)
{
printf
(
"%s\n"
,(*p)[i]);
}
}
//释放内存空间
int
freePoint(
char
***p,
int
count)
{
if
(p==NULL)
{
return
-1;
}
for
(
int
i=0;i<count;i++)
{
free
((*p)[i]);
}
free
(*p);
return
0;
}
int
main(
void
)
{
char
**p = NULL;
int
count = 5;
//分配内存
getMem(&p, count);
//打印数组
printArray(&p, count);
//排序
sort(&p, &count);
//打印数组
printArray(&p, count);
//释放内存
freePoint(&p, count);
return
0;
}
|