Linux C/C++基础——二级指针做形参
2024-09-04 01:32:52
1.二级指针做形参
#include<stdio.h>
#include<stdlib.h> void fun(int **temp)
{
*temp=(int*)malloc(sizeof(int));
**temp=; //可以,但是变量前两个*不常见,常用下面这种
//int *p=(int *)malloc(sizeof(int));
//*p=100;
//*temp=p;
}
int main()
{
int *p=NULL;
fun(&p);
printf("*p=%d\n",*p);
free(p);
p=NULL;
return ;
}
2.值传递1
#include <stdio.h>
#include <stdlib.h> void fun(int *tmp)
{
tmp = (int *)malloc(sizeof(int));
*tmp = ;
} int main(int argc, char *argv[])
{
int *p = NULL;
fun(p); //值传递,形参修改不会影响实参
printf("*p = %d\n", *p);//err,操作空指针指向的内存 return ;
}
值传递2
#include <stdio.h>
#include <stdlib.h> void fun(int *tmp)
{
*tmp = ;
}
int main(int argc, char *argv[])
{
int *p = NULL;
p=(int *)malloc(sizeof(int));
fun(p); //值传递,形参修改不会影响实参
printf("*p = %d\n", *p);
free(p);
p=NULL;
return ;
}
返回堆区地址
#include <stdio.h>
#include <stdlib.h> int *fun()
{
int *tmp = NULL;
tmp = (int *)malloc(sizeof(int));
*tmp = ;
return tmp;//返回堆区地址,函数调用完毕,不释放
} int main(int argc, char *argv[])
{
int *p = NULL;
p = fun();
printf("*p = %d\n", *p);//ok //堆区空间,使用完毕,手动释放
if (p != NULL)
{
free(p);
p = NULL;
} return ;
}
最新文章
- HTML5 之拖放(drag与drop)
- Objective-C精选字符串处理方法
- 【转】单表60亿记录等大数据场景的MySQL优化和运维之道 | 高可用架构
- Storm集群安装部署步骤【详细版】
- SQLserver删除某数据库中所有表 方法 二
- 注册、卸载DLL
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)
- Try out the latest C++ compiler toolset without waiting for the next update of Visual Studio
- javascript-几个基础的排序算法
- 深度优先搜索算法(DFS)以及leetCode的subsets II
- oracle 之 内存—鞭辟近里(一)
- String的Split方法的用法与要注意事项
- MySQL5.7关于密码二三事
- 去中心化存储项目终极指南 | Filecoin, Storj 和 PPIO 项目异同
- 【Nodejs】Nodejsの環境構築
- SSM框架报错分析(一)——There is no getter for property named &#39;XXX&#39; in &#39;class java.lang.String&#39;
- mysql only_full_group_by报错的问题(转)
- altera FIFO知识点
- CodeAction_beta02 斐波那契 (多维DP)
- 【Cesium】坐标理解(转)