ESP-IDF:使用基本类型,指针,引用,指针的指针,指针的引用作为函数参数的几个例程

1.例程:

/下面是使用基本类型,指针,引用,指针的指针,指针的引用作为函数参数的几个例程/

// 值拷贝
int add10(int a)
{
a += 10;
return a;
}
// 指针传参,是一种地址拷贝
void add101(int *a)
{
// int * b = NULL;
// b = a;
// *b +=10;
*a += 10;
}
// 引用传参,也是一种地址拷贝
void add102(int &b)
{
b += 10;
}

// 指针swap
void swap01(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

// 引用swap
void swap02(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

// 给结构体分配空间并赋值返回
struct teacher
{
string name;
int age;
};

// 传入结构体指针的地址,然后分配空间赋值
int allocation002(teacher **te) // te指向(*te), (*te)指向结构体对象。**标识指向指针的指针。
{

teacher *p = (teacher *)malloc(sizeof(teacher));
p->age = 20;
p->name = "laia";
*te = p;
cout << "————在allocation002中使用(**te).name,(**te).age打印结构体信息—————" << endl;
cout << "teacher.name=" << (**te).name << "    teacher.age=" << (**te).age << endl << endl;
return 0;

}
// 传入结构体指针的地址,释放结构体空间,然后把指针赋值为空
void freeAllocation002(teacher **te) // te指向(*te), (*te)指向结构体对象
{
if (te == NULL)
{
return;
}
// teacher *tp = *te;
// if(tp != NULL) {
// free(tp);
// *te = NULL;
// }
if (*te != NULL)
{
free(*te); // 把(*te)指向的结构体内存空间释放掉,free()接收的是指针类型
*te = NULL;
}
}

// 传入结构体指针的引用,给结构体分配空间然后赋值
int allocation003(teacher *&te) // 使用指针的引用类型接收结构体的指针
{

te = (teacher *)malloc(sizeof(teacher)); // te是指针的引用,可以当成指针类型
te->age = 18;
te->name = "kuailaia";
return 0;

}
// 传入结构体指针的引用,释放结构体空间,然后把指针赋值为空
void freeAllocation003(teacher *&te) // 使用指针的引用类型接收结构体的指针
{
if (te != NULL)
{
free(te); // te是指针的引用,可以当成指针类型
te = NULL;
}
}

void test06()
{
int a = 10;
int b = 20;
cout << “a=” << a << endl;
a = add10(a);
cout << “调用add10(int a)后 a=” << a << endl;
add101(&a);
cout << “调用add101(int * a)后 a=” << a << endl;
add102(a);
cout << “调用add102(int & a)后 a=” << a << endl;
cout << “———————————指针swap—————————————” << endl;
cout << “swap01前 a=” << a << " b=" << b << endl;
swap01(&a, &b);
cout << “swap01后 a=” << a << " b=" << b << endl;
cout << “———————————引用swap—————————————” << endl;
cout << “swap01前 a=” << a << " b=" << b << endl;
swap02(a, b);
cout << “swap01后 a=” << a << " b=" << b << endl;

cout << "———————————给结构体分配空间并赋值返回—————————————" << endl;

teacher *p = NULL;
// allocation002(&p); //取结构体指针p的地址,然后匿名传入函数参数,当然也可以写成下面两行
teacher **pp = &p;
allocation002(pp);

cout << "———————————调用allocation002(&te)后te的值—————————————" << endl;
cout << "teacher.name=" << p->name << "    teacher.age=" << p->age << endl << endl;
freeAllocation002(&p); // 传入结构体指针的地址

teacher *te3 = NULL;
cout << "———————————调用allocation003(&te)后te的值—————————————" << endl;
allocation003(te3); // 把指针作为参数传入,allocation003()使用指针的引用类型接收结构体的指针
cout << "teacher.name=" << te3->name << "    teacher.age=" << te3->age << endl << endl;
freeAllocation003(te3);

}

extern “C” void app_main(void)
{
test06();
}

2.结果:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值