1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#
include
<stdio.h>
int
max(
int
*,
int
*);
int
main(
void
)
{
int
a, b, max_num;
int
*pa, *pb;
int
(*p)(
int
* ,
int
*);
printf(
"Please input a:"
);
scanf(
"%d"
, &a);pa = &a;
printf(
"Please input b:"
);
scanf(
"%d"
, &b);pb = &b;
p = max;
//let p point to max funtion.
max_num = (*p)(pa, pb);
//call the funtion through pointer p.
// max_num = max(pa, pb);
printf(
"The max number is:%d\n"
, max_num);
return
0
;
}
|
1
2
3
4
5
6
7
8
|
#
include
<stdio.h>
int
max(
int
*pa,
int
*pb)
{
if
(*pa >= *pb)
return
*pa;
else
return
*pb;
}
|
1
2
3
4
5
6
|
xpleaf@leaf:~/stuc/fun$ gcc -c max.c max_fun.c
xpleaf@leaf:~/stuc/fun$ gcc -o max max.o max_fun.o
xpleaf@leaf:~/stuc/fun$ ./max
Please input a:
3
Please input b:
4
The max number
is
:
4
|
1
2
|
int
(*p)(
int
*,
int
*)
类型名 (*指针变量名)(函数参数列表);
|
1
2
|
p = max; ===>赋值:把函数入口地址赋给函数指针
max_num = (*p)(pa, pb); ===>调用:将原来直接的max函数调用改变为(*p)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#
include
<stdio.h>
int
sum(
int
*,
int
*);
int
max(
int
*,
int
*);
int
min(
int
*,
int
*);
int
main(
void
)
{
int
a, b, choice, *pa, *pb;
printf(
"Enter two number and choose what do you want to do.\n"
);
printf(
"Please enter a:"
);scanf(
"%d"
, &a);pa = &a;
printf(
"Please enter b:"
);scanf(
"%d"
, &b);pb = &b;
printf(
"What do you want to do?\n"
);
printf(
"1.Add two number.\n2.Return the max.\n3.Return the min.\n"
);
scanf(
"%d"
,&choice);
if
(choice ==
1
)
fun(sum, pa, pb);
else
if
(choice ==
2
)
fun(max, pa, pb);
else
if
(choice ==
3
)
fun(min, pa, pb);
return
0
;
}
|
1
2
3
4
5
6
7
|
#
include
<stdio.h>
void
fun(
int
(*p)(
int
*,
int
*),
int
*pa,
int
*pb)
{
int
result;
result = (*p)(pa, pb);
printf(
"The result is:%d\n"
, result);
}
|
1
2
3
4
5
|
#
include
<stdio.h>
int
sum(
int
*pa,
int
*pb)
{
return
*pa+*pb;
}
|
1
2
3
4
5
6
7
8
|
#
include
<stdio.h>
int
max(
int
*pa,
int
*pb)
{
if
(*pa >= *pb)
return
*pa;
else
return
*pb;
}
|
1
2
3
4
5
6
7
8
|
#
include
<stdio.h>
int
min(
int
*pa,
int
*pb)
{
if
(*pa <= *pb)
return
*pa;
else
return
*pb;
}
|
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
|
xpleaf@leaf:~/stuc/fun$ gcc -c mix.c fun.c sum.c max.c min.c
xpleaf@leaf:~/stuc/fun$ gcc -o mix mix.o fun.o sum.o max.o min.o
xpleaf@leaf:~/stuc/fun$ ./mix
Enter two number and choose what
do
you want to
do
.
Please enter a:
3
Please enter b:
4
What
do
you want to
do
?
1
.Add two number.
2
.Return the max.
3
.Return the min.
1
The result
is
:
7
xpleaf@leaf:~/stuc/fun$ ./mix
Enter two number and choose what
do
you want to
do
.
Please enter a:
3
Please enter b:
4
What
do
you want to
do
?
1
.Add two number.
2
.Return the max.
3
.Return the min.
2
The result
is
:
4
xpleaf@leaf:~/stuc/fun$ ./mix
Enter two number and choose what
do
you want to
do
.
Please enter a:
3
Please enter b:
4
What
do
you want to
do
?
1
.Add two number.
2
.Return the max.
3
.Return the min.
3
The result
is
:
3
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#
include
<stdio.h>
int
*max(
int
*,
int
*);
int
main(
void
)
{
int
a, b;
int
*pa, *pb, *pmax;
int
*(*p)(
int
* ,
int
*);
printf(
"Please input a:"
);
scanf(
"%d"
, &a);pa = &a;
printf(
"Please input b:"
);
scanf(
"%d"
, &b);pb = &b;
p = max;
//let p point to max funtion.
pmax = (*p)(pa, pb);
//call the funtion through pointer p.
// pmax = max(pa, pb);
printf(
"The max number is:%d\n"
, *pmax);
return
0
;
}
|
1
2
3
4
5
6
7
8
|
#
include
<stdio.h>
int
*max(
int
*pa,
int
*pb)
{
if
(*pa >= *pb)
return
pa;
else
return
pb;
}
|
1
2
3
4
5
6
|
xpleaf@leaf:~/stuc/fun$ gcc -c max.c max_fun.c
xpleaf@leaf:~/stuc/fun$ gcc -o max max.o max_fun.o
xpleaf@leaf:~/stuc/fun$ ./max
Please input a:
3
Please input b:
4
The max number
is
:
4
|
1
2
|
int
*max(
int
*,
int
*);
类型名 *函数名(参数列表);
|
1
2
|
int
*(*p)(
int
* ,
int
*);
类型名 *(*指针名)(参数列表);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#
include
<stdio.h>
int
main(
void
)
{
int
i;
char *book[] = {
"Python"
,
"C"
,
"Linux"
,
"Centos"
,
"Ubuntu"
};
for
(i =
0
;i <
5
;i++)
printf(
"%s\n"
, book[i]);
return
0
;
}
|
1
2
3
4
5
6
7
8
|
xpleaf@leaf:~/stuc/arry$ gcc -c arry.c
xpleaf@leaf:~/stuc/arry$ gcc -o arry_book arry.o
xpleaf@leaf:~/stuc/arry$ ./arry_book
Python
C
Linux
Centos
Ubuntu
|
1
2
|
char *book[
10
];
类型名 *数组名[数组长度];
|
1
|
int
(*p)[
4
]; ===>指向含有
4
个元素的一维数组的指针
|
1
2
3
|
printf(
"%s\n"
, book[i]);
改为:
printf(
"%s\n"
, *(book+i));
|