C++之函数体——通过指针引用

使用数组作为函数的参数,在C++之函数体那里已经讲了通过普通的方法将数组作为参数,引入到函数中。这里介绍使用指针的方法将数组作为参数

  • 整数。

单个的数需要加&表示指针变量存储的是变量地址
特别强调:指针一定慎用(*ptr_num)++,会改变指针所指向数的数值,使用指针最好配合const禁止数值被修改!!! 这里另写一篇文章做参考!!

#include <iostream>
#include "class1demo.hpp"
using namespace std;

//声明函数
void sum2(int*, int*);

int main()
{
    int num1 = 10;
    int num2 = 20;
    sum2(&num1, &num2);//调用函数,传入时需要加 & 取址符号
    return 0;
}
//定义函数
void sum2(int* ptr_num1, int* ptr_num2)
{
    int res = 0;
    res = *ptr_num1 + *ptr_num2;
    //(*ptr_num1)++;//相当于把num1的值给修改了,后面函数再使用num1时,其实num1 = 11了!!!
    cout << res << endl;
    //cout << *ptr_num1 << endl;
}

//程序输出

30

Process returned 0 (0x0)   execution time : 0.160 s
Press any key to continue.

  • 一维数组(1)。

指针指向数组是,不用加&,默认指向数组第一个元素

//1、头文件constdemo.h

#ifndef CONSTDEMO_H_INCLUDED
#define CONSTDEMO_H_INCLUDED
#include <iostream>
using namespace std;

#endif // CONSTDEMO_H_INCLUDED
void show_ptr(int* , int);//声明函数,数组指针,第二个元素作为数组长度

void show_ptr(int* value_ptr, int len)//定义函数体
{
    for(int i = 0; i < len; i++)//打印数组中的元素
    {
         cout << *(value_ptr + i) << "\t";
    }
}

//2、主文件main.cpp

#include <iostream>
#include <cmath>
#include "headdemo.h"
#include "constdemo.h"//引入上面头文件,用过双引号引用
using namespace std;

int main()
{
    int values[] = {1,2,3,4,5};
    int len = sizeof(values) / sizeof(int);//数组长度
    show_ptr(values,len);
    return 0;
}

//程序输出

1       2       3       4       5
Process returned 0 (0x0)   execution time : 0.063 s
Press any key to continue.
  • 一维数组(2)方法二
    //1、头文件constdemo.h
#ifndef CONSTDEMO_H_INCLUDED
#define CONSTDEMO_H_INCLUDED
#include <iostream>
using namespace std;

#endif // CONSTDEMO_H_INCLUDED
//方法二
void show_ptr2(int* , int*);//声明函数

void show_ptr2(int* value_ptr_start, int* value_ptr_end)//定义函数,传入的形参为指针
{
    for(int* i = value_ptr_start; i <= value_ptr_end; i++)//这里通过定义指针i作为变量,进行数组的遍历
    {
         cout << *i << "\t";
    }
}

//2、主文件main.cpp

#include <iostream>
#include <cmath>
#include "headdemo.h"
#include "constdemo.h"//引入上面头文件,用过双引号引用
using namespace std;

int main()
{
    int values[] = {1,2,3,4,5};
    int len = sizeof(values) / sizeof(int);
    //show_ptr(values,len);
    show_ptr2(values, values + len - 1);//穿入首指针和尾指针
    return 0;
}

//程序输出

1       2       3       4       5
Process returned 0 (0x0)   execution time : 0.167 s
Press any key to continue.
  • 二维数组
    //1、头文件constdemo.h
#ifndef CONSTDEMO_H_INCLUDED
#define CONSTDEMO_H_INCLUDED
#include <iostream>
using namespace std;

#endif // CONSTDEMO_H_INCLUDED

void show_ptr2(int * , int);//声明二维数组函数

void show_ptr2(int (*value_ptr)[4], int len)//定义二维数组函数,这里传入的指针维数要进行改变
{
    for(int i = 0; i < len; i++)//外循环控制行
    {
         for(int j = 0; j < 4; j++)//内循环控制列
         {
             cout << *(*(value_ptr + i) + j) << "\t";
         }
         cout << endl;
    }
}


//2、主文件main.cpp

#include <iostream>
#include <cmath>
#include "headdemo.h"
#include "constdemo.h"//引入上面头文件,用过双引号引用
using namespace std;

int main()
{
    int (*ptr_nums)[4] = new int[3][4];//用new方法开辟3行4列数组
    show_ptr2(ptr_nums , 3);
    delete ptr_nums;//测试时与new成对出现,避免出现野指针
    
    return 0;
}

//程序输出

131264  136456  0       0
0       0       0       0
0       0       0       0

Process returned 0 (0x0)   execution time : 0.102 s
Press any key to continue.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值