当前位置:网站首页>指针函数(基础)
指针函数(基础)
2022-07-05 04:16:00 【曹乐乐爱学习】
写一个指针类型的函数,返回一个指针,但是:

这个之前有解释过:

所以,在函数调用时候,返回值,是没有问题的;因为在函数中返回值的地址空间在被释放之前,值就已经赋值给了函数名,而返回地址时候,这块地址空间已经被释放了(个人理解,未必正确)。
要想返回地址没有问题:
1、设置全局变量——不建议
2、设置静态变量——视情况而定()

对比看一下:
返回值:

返回地址(返回值是函数指针):
局部变量,不能返回地址再取值:
要修改存储类型,使其在函数调用之后,内存不被释放:

附:字符串连拷贝——将通同样的数据内容,从一个地址空间,拷贝到另一个地址空间
函数原型 char *strcpy(char *dest, const char *src);
函数指针练习:模仿strcat函数,实现自己的字符串连接

注意:自增运算符的优先级>取值优先级,所以*str++ ==> *(str++)
与 i++一样,只不过一个是变量i,一个是指针变量*p;其实都是先用,再加:
i++ 是先用i的值,再将i的值++
*p++ 是先用*p的值,再将p的地址++
拆开来写就是:
*str
str++

因此:*str1++ = *str2++; 其实就是 *(str1++) *(str2++);
要理清楚的 :*(str1++) *(str2++);可不是str2地址递增,str2地址递增,然后取str2递增后的值,赋值给str1地址递增后的位置。而是与 a++ = i++一样
先各自取值,再赋值,然后再地址递增:
*str1 =*str2;
str2++;
srt1++;
------------------------------------------------------------------------------------------------------------------
练习2:模仿字符串转整形函数:atoi。

写一个自己的整形转字符串:inttostr

思路:
1、先用循环对整数取余,并移除掉最低位
2、字符’0‘与整数0,相差48,也就是’0‘
3、将筛选出来的每一位,存到数组里(这时候是反的,最低位跑到了最左边)
4、交换
(由于aa[i],在转换完成后,人为补了一个’\0‘,所以i--,走到最后一个字符aa[i],再找一个变量n从数组aa[0]开始交换,并往中间走。)
于是:
倒数第一个字符与第一个字符交换
倒数第二个字符与第二个字符交换
........随着i--;j++;若i=j,则数据为奇数个;若i<j,则说明数据为偶数个
如果是奇数个,i=j时候,最中间的字符不用动;偶数个时候,当i<j时候,最中间的两个数据已经被交换。
还有一个问题:上面代码中,函数中定义了一个静态数组,大小是固定的;那么,如果参数传递一个数组进来,是不是更好?

边栏推荐
- Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
- FFmepg使用指南
- Ffmepg usage guide
- open graph协议
- [charging station]_ Secular wisdom_ Philosophical wisdom _
- Realize the attention function of the article in the applet
- Online sql to excel (xls/xlsx) tool
- [brush questions] BFS topic selection
- Machine learning decision tree
- Plasticscm enterprise crack
猜你喜欢

【thingsboard】替换首页logo的方法

10种寻址方式之间的区别
![[phantom engine UE] the difference between running and starting, and the analysis of common problems](/img/e2/49d6c4777c12e9f4e3f8b6ca6db41c.png)
[phantom engine UE] the difference between running and starting, and the analysis of common problems

This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises

SPI read / write flash principle + complete code

阿里云ECS使用cloudfs4oss挂载OSS

Is "golden nine and silver ten" the best time to find a job? Not necessarily

ActiveReportsJS 3.1 VS ActiveReportsJS 3.0

Wechat applet development process (with mind map)

Rome链分析
随机推荐
Threejs realizes sky box, panoramic scene, ground grass
Kwai, Tiktok, video number, battle content payment
如何优雅的获取每个分组的前几条数据
A real day for Beijing programmers!!!!!
ClickPaaS低代码平台
10种寻址方式之间的区别
open graph协议
IronXL for . NET 2022.6
Hexadecimal to octal
Summary of scene design
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
Un réveil de l'application B devrait être rapide
kubernetes集群之调度系统
MacBook installation postgresql+postgis
Containerd series - what is containerd?
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
A solution to the problem that variables cannot change dynamically when debugging in keil5
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
Plasticscm enterprise crack
【虚幻引擎UE】实现测绘三脚架展开动画制作