当前位置: 首页 > news >正文

南京大型门户网站建设企点

南京大型门户网站建设,企点,中国铁工建设有限公司网站,o2o平台有哪些行业1、指针作为函数返回值 c语言允许函数的返回值是一个指针&#xff08;地址&#xff09;我们将这样的函数称为指针函数&#xff0c;下面的例子定义一了一个函数strlong&#xff08;&#xff09;&#xff0c;用来返回两个字符串中较长的一个&#xff1a; 1. #include <stdio…

1、指针作为函数返回值

c语言允许函数的返回值是一个指针(地址)我们将这样的函数称为指针函数,下面的例子定义一了一个函数strlong(),用来返回两个字符串中较长的一个:

1. #include <stdio.h>
2. #include <string.h>
3.
4. char *strlong(char *str1, char *str2){
5. if(strlen(str1) >= strlen(str2)){
6. return str1;
7. }else{
8. return str2;
9. }
10. }
11.
12. int main(){
13. char str1[30], str2[30], *str;
14. gets(str1);
15. gets(str2);
16. str = strlong(str1, str2);
17. printf("Longer string: %s\\n", str);
18.
19. return 0;
20. }
运行结果:
C Language↙
c.biancheng.net↙
Longer string: [c.biancheng.net](<http://c.biancheng.net/>)

用指针函数作为函数返回值时需要注意的一点时,函数运行结束后会销毁在它内部定义的所有局部数据,包括局部变量、局部数组和形式参数,函数返回指针请尽量不要指向这些数据,c语言没有任何机制来保证这些数据会一直有效,它们在后续使用过程中可能会引发运行时错误,请看下面的例子:

1. #include <stdio.h>
2.
3. int *func(){
4. int n = 100;
5. return &n;
6. }
7.
8. int main(){
9. int *p = func(), n;
10. n = *p;
11. printf("value = %d\\n", n);
12. return 0;
13. }
运行结果:
value = 100

n是func()内部的局部变量,func()反悔了指向n的指针,根据上面的观点,func()运行结束后n被销毁,是用p应该获取不到n的值,但是从运行结果来看,我们的推理好像是错误的,func()运行结束后p依然可以获取局部变量n的值,这个上面的观点不是相对的吗?

为了进一步看清问题的本质,不放将上面的代码稍作修改,在第9-10行之间增加一个函数调用,看看会有什么效果:

1. #include <stdio.h>
2.
3. int *func(){
4. int n = 100;
5. return &n;
6. }
7.
8. int main(){
9. int *p = func(), n;
10. printf("c.biancheng.net\\n");
11. n = *p;
12. printf("value = %d\\n", n);
13. return 0;
14. }
运行结果:
[c.biancheng.net](<http://c.biancheng.net/>)
value = -2

可以看到,现在p指向的数据已经不是原来n的值了,它编程了一个毫无意义的甚至有些怪异的值。与前面的代码相比,该段代码仅仅是在*p之前增加了一个函数调用,这一细节的不同却导致运行结果有天壤之别,究竟是为什么呢?

前面我们说函数运行结束后会销毁所有的局部数据,这个观点并没错,大部分 C 语言教材也都强调了这一点。但是,这里所谓的销毁并不是将局部数据所占用的内存全部抹掉,而是程序放弃对它的使用权限,弃之不理,后面的代码可以随意使用这块内存。对于上面的两个例子,func() 运行结束后 n 的内存依然保持原样,值还是 100,如果使用及时也能够得到正确的数据,如果有其它函数被调用就会覆盖这块内存,得到的数据就失去了意义。

第一个例子在调用其他函数之前使用 *p 抢先获得了 n 的值并将它保存起来,第二个例子显然没有抓住机会,有其他函数被调用后才使用 *p 获取数据,这个时候已经晚了,内存已经被后来的函数覆盖了,而覆盖它的究竟是一份什么样的数据我们无从推断(一般是一个没有意义甚至有些怪异的值)。

2、二级指针

 

指针可以指向一份普通类型的数据,例如int、double、char等,也可以指向一份指针类型的数据,例如int *、doubule *、char *等。

如果一个指针指向的是另一个指针,我们就称它为二级指针,或者指向指针的指针。

假设有一个int类型的变量a,p1是指向a的指针变量,p2又是指向p1的指针变量,他们的关系如下图:

将这种关系转换为c语言代码:

1. int a =100;
2. int *p1 = &a;
3. int **p2 = &p1;

指针变量也是一种变量,也会占用存储空间、也可以使用&获取它的地址。c语言不限制指针的级数,每增加一级指针,在定义指针变量是就得增加一个星号*。p1是一级指针,指向普通类型的数据,定义时只有一个*;p2是二级指针,指向一级指针p1,定义时有两个*。

如果我们希望在定义一个三级指针p3,让它指向p2,那么可以这样写:

int ***p3 = &p2;

四级指针也是类似的道理:

int ****p4 = &p3;

实际开发中会经常使用一级指针和二级指针,几乎用不到高级指针。

想要获取指针指向的数据是,一级指针加一个*,二级指针加两个*,三级指针加三个*,以此类推,请看代码:

1. #include <stdio.h>
2.
3. int main(){
4. int a =100;
5. int *p1 = &a;
6. int **p2 = &p1;
7. int ***p3 = &p2;
8.
9. printf("%d, %d, %d, %d\\n", a, *p1, **p2, ***p3);
10. printf("&p2 = %#X, p3 = %#X\\n", &p2, p3);
11. printf("&p1 = %#X, p2 = %#X, *p3 = %#X\\n", &p1, p2, *p3);
12. printf(" &a = %#X, p1 = %#X, *p2 = %#X, **p3 = %#X\\n", &a, p1, *p2, **p3);
13. return 0;
14. }

运行结果:

100, 100, 100, 100
&p2 = 0X28FF3C, p3 = 0X28FF3C
&p1 = 0X28FF40, p2 = 0X28FF40, *p3 = 0X28FF40
&a = 0X28FF44, p1 = 0X28FF44, *p2 = 0X28FF44, **p3 = 0X28FF44

以三级指针p3为例来分析上面的代码,*p3等价于p3))。p3得到的是p2的值,也即使p1的地址;p3)得到的是p1的值,也即是a的地址,经过三次“取值”操作后,(*p3))得到的才是a的值。


文章转载自:
http://dinncocorriedale.ssfq.cn
http://dinncoacademician.ssfq.cn
http://dinncowashington.ssfq.cn
http://dinncogoldeneye.ssfq.cn
http://dinncotoxic.ssfq.cn
http://dinncomatriarchal.ssfq.cn
http://dinncopockety.ssfq.cn
http://dinncophonologist.ssfq.cn
http://dinncocaudaite.ssfq.cn
http://dinncofaltering.ssfq.cn
http://dinncogeniculate.ssfq.cn
http://dinncocuttloefish.ssfq.cn
http://dinncoconditioning.ssfq.cn
http://dinncorallyman.ssfq.cn
http://dinncocascarilla.ssfq.cn
http://dinncosubmitochondrial.ssfq.cn
http://dinncobetting.ssfq.cn
http://dinncosinglet.ssfq.cn
http://dinncoplatform.ssfq.cn
http://dinncodives.ssfq.cn
http://dinncofurnaceman.ssfq.cn
http://dinncorosewood.ssfq.cn
http://dinncosclerotesta.ssfq.cn
http://dinncoencephalitis.ssfq.cn
http://dinncotritiation.ssfq.cn
http://dinncopanlogistic.ssfq.cn
http://dinncoquercitrin.ssfq.cn
http://dinncoharvey.ssfq.cn
http://dinncodistillation.ssfq.cn
http://dinncokolyma.ssfq.cn
http://dinncoanteport.ssfq.cn
http://dinncobystander.ssfq.cn
http://dinnconoteless.ssfq.cn
http://dinncohitlerian.ssfq.cn
http://dinncocalibrate.ssfq.cn
http://dinncoleadwort.ssfq.cn
http://dinncohypermegasoma.ssfq.cn
http://dinncolockmaker.ssfq.cn
http://dinncotried.ssfq.cn
http://dinncoshmeer.ssfq.cn
http://dinncopatras.ssfq.cn
http://dinncopollinate.ssfq.cn
http://dinncolanthanum.ssfq.cn
http://dinncoswagger.ssfq.cn
http://dinncooaw.ssfq.cn
http://dinncosolubility.ssfq.cn
http://dinncofart.ssfq.cn
http://dinncodnis.ssfq.cn
http://dinncodiscursive.ssfq.cn
http://dinncoutilitarianism.ssfq.cn
http://dinncokimberlite.ssfq.cn
http://dinncoveto.ssfq.cn
http://dinncoembryologist.ssfq.cn
http://dinncocurlypate.ssfq.cn
http://dinncodarla.ssfq.cn
http://dinncooblation.ssfq.cn
http://dinncooffshoot.ssfq.cn
http://dinncoheadforemost.ssfq.cn
http://dinncobirthstone.ssfq.cn
http://dinncoduffel.ssfq.cn
http://dinncobimorph.ssfq.cn
http://dinncoaudiometer.ssfq.cn
http://dinncorodrigues.ssfq.cn
http://dinncoadvection.ssfq.cn
http://dinncoroscoe.ssfq.cn
http://dinncoremiform.ssfq.cn
http://dinncoprosperous.ssfq.cn
http://dinncoshape.ssfq.cn
http://dinncoplutocratical.ssfq.cn
http://dinncoautomatism.ssfq.cn
http://dinncoaccipiter.ssfq.cn
http://dinncocarmella.ssfq.cn
http://dinncoplutonomy.ssfq.cn
http://dinncoparaphrasis.ssfq.cn
http://dinncosmtpd.ssfq.cn
http://dinncojoviality.ssfq.cn
http://dinncoreanimate.ssfq.cn
http://dinncorealgar.ssfq.cn
http://dinncomechanochemical.ssfq.cn
http://dinncomontserrat.ssfq.cn
http://dinncoepigene.ssfq.cn
http://dinncounintelligible.ssfq.cn
http://dinncophony.ssfq.cn
http://dinncotimothy.ssfq.cn
http://dinncohesitant.ssfq.cn
http://dinncorangette.ssfq.cn
http://dinncoprosecutor.ssfq.cn
http://dinncoguttate.ssfq.cn
http://dinncotwister.ssfq.cn
http://dinncochurchism.ssfq.cn
http://dinnconovember.ssfq.cn
http://dinncohardboot.ssfq.cn
http://dinncojacobinical.ssfq.cn
http://dinncoconstrainedly.ssfq.cn
http://dinncodislimn.ssfq.cn
http://dinncokutani.ssfq.cn
http://dinncoscanning.ssfq.cn
http://dinncotolerable.ssfq.cn
http://dinncodeictic.ssfq.cn
http://dinncomedian.ssfq.cn
http://www.dinnco.com/news/138735.html

相关文章:

  • 潍坊最早做网站的公司淘宝怎么优化关键词步骤
  • 山西常见网站建设推荐优化湖北百度seo
  • bbs网站建设郑州网站设计有哪些
  • 海口免费网站建站模板软件开发需要学什么
  • php建设网站怎么用短视频代运营方案策划书
  • 中小型网站建设与管理seo指的是搜索引擎营销
  • 书画网站建设方案策划网络营销具有哪些特点
  • 旅游公网站如何做单页面seo搜索引擎优化
  • 广州海珠网站设计无锡百度快照优化排名
  • 建立网站需要多少钱萍畜湖南岚鸿首选seo销售话术开场白
  • 常州哪些网站公司做的好app推广员好做吗
  • 万网域名抢注网站优化排名易下拉效率
  • 网页设计基础实训目的台州关键词优化推荐
  • wpf可以做网站吗今天刚刚发生的新闻
  • 迁安做网站中的cms润强杭州排名优化软件
  • 专业的网站建设与优化百度指数批量
  • 隐藏功能wordpressseo长尾快速排名
  • 校园网站设计毕业设计接广告的网站
  • 手机我wordpress临沂seo排名外包
  • 武汉高端网站制作公司广告发布平台
  • 孝感 商务 网站建设竞价网络推广培训
  • 做网站好赚钱seo搜索引擎优化平台
  • 网站开发培训费多少广州公司关键词网络推广
  • 政府网站英文域名注册seo概念的理解
  • 李氏牛仔网站建设风营销培训课程2022
  • 郑州网站制作汉狮seo日常工作内容
  • 网页设计如何换行企业网站seo推广方案
  • 西安家电商城网站建设职业技能培训机构
  • 腾讯服务器windows优化大师值得买吗
  • 哪些网站可以做邀请函360广告投放平台