「C语言回顾之旅」第二篇:指针详解进阶

简介:

说明:

    第一篇回顾了指针的基本概念以及基本使用,因此对指针也有了一个较为清晰的思路,但实际上第一篇关于指针的内容是不太容易忘记的。这是第二篇中的内容是比较容易混淆,但对于指针的进一步学习也是非常重要的。





一.指向函数的指针


1.函数指针


·函数指针即指向函数的指针,函数指针值为函数的入口地址,通过使用该指针,即可以使用该函数;

·编写一个程序返回两个数的最大值,通过函数指针调用函数:

a.main函数代码如下:

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 ;
}

b.max函数代码如下:

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;
}

c.执行过程如下:

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)




2.用函数指针作函数参数


·使用函数指针的重要作用是把函数的地址作为参数传递到其他函数;

·编写一个程序,用函数指针作函数参数,让用户选择执行不同的功能:

a.main函数的代码如下:

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 ;
}

b.调用其他子函数的fun函数代码如下:

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);
}

c.返回和的sum函数代码如下:

1
2
3
4
5
# include <stdio.h>
int  sum( int  *pa,  int  *pb)
{
   return  *pa+*pb;
}

d.返回最大值的max函数代码如下:

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;
}

e.返回最小值的min函数代码如下:

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;
}

f.执行过程如下:

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.指针函数


·指针函数即返回值为指针的函数;

·改写函数指针中的第一个程序,使函数的返回值为一指针:

a.main函数代码如下:

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 ;
}

b.返回最大值函数代码如下:

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;
}

c.执行过程如下:

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.指针数组


·存放指针值的数组即为指针数组;

·编写一个程序,通过指针数组来存放书名:

a.main函数代码如下:

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 ;
}

b.执行过程如下:

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


·为便于理解,可用如下示意图形象化:

wKiom1X1MqLxI57NAADBE7bbQ-w498.jpg


·由此可知,定义指针数组的形式为:

1
2
char *book[ 10 ];
类型名 *数组名[数组长度];

·注意与指向二维数组指针的区别:

1
int  (*p)[ 4 ];    ===>指向含有 4 个元素的一维数组的指针

·可通过符号的优先级进行区分记忆,[ ]优先级比*高,指针数组[ ]先结合,是数组,元素值为指针;指向二维数组的指针,*先结合,是指针,指向含有4个元素的一维数组;




2.指向指针数据的指针


·因为数组的处理在编译过程中是按照指针处理的,对于上面的程序,各改为为如下:

1
2
3
printf( "%s\n" , book[i]);
改为:
printf( "%s\n" , *(book+i));

·这与正常一维数组的处理是一致的,只是数组存放的也是指针,可能会有一混淆,可用下面图示形象理解:

wKiom1X1N5OzEWrvAAE2Kexbb9I279.jpg

·可进一步简化为:

wKiom1X1OezSSTXVAABJ0K6oTs8805.jpg

·上面的形式为:指针-->指针(字符串首地址)-->字符串首字符,实则为多重指针;





三.动态内存分配与指向它的指针变量














<未完,continue>

相关文章
|
3天前
|
人工智能 自然语言处理 Shell
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
仅用3分钟,百炼调用满血版Deepseek-r1 API,享受百万免费Token。阿里云提供零门槛、快速部署的解决方案,支持云控制台和Cloud Shell两种方式,操作简便。Deepseek-r1满血版在推理能力上表现出色,尤其擅长数学、代码和自然语言处理任务,使用过程中无卡顿,体验丝滑。结合Chatbox工具,用户可轻松掌控模型,提升工作效率。阿里云大模型服务平台百炼不仅速度快,还确保数据安全,值得信赖。
157353 24
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
|
5天前
|
人工智能 API 网络安全
用DeepSeek,就在阿里云!四种方式助您快速使用 DeepSeek-R1 满血版!更有内部实战指导!
DeepSeek自发布以来,凭借卓越的技术性能和开源策略迅速吸引了全球关注。DeepSeek-R1作为系列中的佼佼者,在多个基准测试中超越现有顶尖模型,展现了强大的推理能力。然而,由于其爆火及受到黑客攻击,官网使用受限,影响用户体验。为解决这一问题,阿里云提供了多种解决方案。
16987 37
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
5天前
|
并行计算 PyTorch 算法框架/工具
本地部署DeepSeek模型
要在本地部署DeepSeek模型,需准备Linux(推荐Ubuntu 20.04+)或兼容的Windows/macOS环境,配备NVIDIA GPU(建议RTX 3060+)。安装Python 3.8+、PyTorch/TensorFlow等依赖,并通过官方渠道下载模型文件。配置模型后,编写推理脚本进行测试,可选使用FastAPI服务化部署或Docker容器化。注意资源监控和许可协议。
1310 8
|
13天前
|
人工智能 搜索推荐 Docker
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
DeepSeek R1 + LobeChat + Ollama:快速本地部署模型,创建个性化 AI 助手
3416 117
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
|
8天前
|
人工智能 自然语言处理 API
DeepSeek全尺寸模型上线阿里云百炼!
阿里云百炼平台近日上线了DeepSeek-V3、DeepSeek-R1及其蒸馏版本等六款全尺寸AI模型,参数量达671B,提供高达100万免费tokens。这些模型在数学、代码、自然语言推理等任务上表现出色,支持灵活调用和经济高效的解决方案,助力开发者和企业加速创新与数字化转型。示例代码展示了如何通过API使用DeepSeek-R1模型进行推理,用户可轻松获取思考过程和最终答案。
|
5天前
|
人工智能 自然语言处理 程序员
如何在通义灵码里用上DeepSeek-V3 和 DeepSeek-R1 满血版671B模型?
除了 AI 程序员的重磅上线外,近期通义灵码能力再升级全新上线模型选择功能,目前已经支持 Qwen2.5、DeepSeek-V3 和 R1系列模型,用户可以在 VSCode 和 JetBrains 里搜索并下载最新通义灵码插件,在输入框里选择模型,即可轻松切换模型。
934 14
|
12天前
|
API 开发工具 Python
阿里云PAI部署DeepSeek及调用
本文介绍如何在阿里云PAI EAS上部署DeepSeek模型,涵盖7B模型的部署、SDK和API调用。7B模型只需一张A10显卡,部署时间约10分钟。文章详细展示了模型信息查看、在线调试及通过OpenAI SDK和Python Requests进行调用的步骤,并附有测试结果和参考文档链接。
1938 9
阿里云PAI部署DeepSeek及调用
|
9天前
|
人工智能 数据可视化 Linux
【保姆级教程】3步搞定DeepSeek本地部署
DeepSeek在2025年春节期间突然爆火出圈。在目前DeepSeek的网站中,极不稳定,总是服务器繁忙,这时候本地部署就可以有效规避问题。本文以最浅显易懂的方式带读者一起完成DeepSeek-r1大模型的本地部署。

热门文章

最新文章