博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验9-4 函数的返回值为指针变量
阅读量:4145 次
发布时间:2019-05-25

本文共 609 字,大约阅读时间需要 2 分钟。

实验目的:

进一步理解野指针。

实验步骤:

1.分析下面的程序(例9-9)。

#include <stdio.h>

int *test( )

{

          int i = 5, *pi = &i;

          return pi;

}

void test2( )

{

         int j = 3;

}

void main( )

{

         int *pj;

          pj = test( );

          test2( );

          printf("%d\n", *pj);

}

分析:

程序的输出结果为3,不是5,为什么?

2.比较下面两个程序。

2.1

#include <stdio.h>

void test(int *pi)

{

       *pi= 5;

}

void main( )

{

       int*p;

       test(p);

       printf("程序中其它功能……\n");

}

2.2

#include <stdio.h>

void test(int *pi)

{

       if(pi== NULL)

       {

              printf("错误!实参为空指针!\n");

              return;

       }

       *pi= 5;

}

void main( )

{

       int*p = NULL;

       test(p);

       printf("程序中其它功能……\n");

}

分析:

1)两个程序中有语法错误吗?

2)两个程序运行结果相同吗?为什么?

注意:

在函数中检测指针参数是否为空指针会提高代码的健壮性。负责的程序员必须保证指针变量指向合法的地址。

转载地址:http://lddti.baihongyu.com/

你可能感兴趣的文章
设计模式之二:工厂方法模式
查看>>
设计模式之三:抽象工厂模式
查看>>
设计模式之四:模板方法模式
查看>>
设计模式之五:建造者模式
查看>>
设计模式之六:代理模式
查看>>
设计模式之七:原型模式
查看>>
设计模式之八:中介者模式
查看>>
高通vuforia动态加载、卸载、对焦、翻转摄像头、控制识别数模板
查看>>
unity手势控制、放大缩小、拖拽、滑动。单击
查看>>
unity批量更改assetbundle名称、清除名称打包脚本
查看>>
二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
查看>>
Unity导入资源(纹理 Texture 声音 Audio 模型 FBX)动态设置
查看>>
高通 Vuforia 最新6-2-10版本 API 动态加载、卸载识别库 闪光灯 对焦 翻转摄像头 动态更改识别数量等
查看>>
解决AnimationClip.SetCurve RectTransform Color参数 出现Missing!的情况
查看>>
Convolutional Neural Network Architectures for Matching Natural Language Sentences论文笔记
查看>>
Task-oriented Dialogue System for Automatic Diagnosis论文笔记
查看>>
FreeAnchor: Learning to Match Anchors for Visual Object Detection论文详解
查看>>
pycharm+Docker+GPU配置
查看>>
Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0) CUDNN_STATUS_BAD_PARAM
查看>>
关于loss不收敛的一些建议
查看>>