详解C语言二级指针三种内存模型
二级指针相对于一级指针,显得更难,难在于指针和数组的混合,定义不同类型的二级指针,在使用的时候有着很大的区别
第一种内存模型char *arr[]
若有如下定义
char *arr[] = {"abc", "def", "ghi"};这种模型为二级指针的第一种内存模型,在理解的时候应该这样理解:定义了一个指针数组(char * []),数组的每个元素都是一个地址。
在使用的时候,若要使用中间量操作元素,那么此时中间量应该定义为
char *tmp = NULL;如果要打印这个数组,那么可以使用以下函数
int printAarray(char **pArray, int num) { int i = 0; if (pArray == NULL) { return -1; } for (i = 0; i < num; i++) { printf("%s \n", pArray[i]); } return 0; }第二种内存模型char arr[][]
若有如下定义
char arr[3][5] = {"abc", "def", "ghi"};这种模型为二级指针的第二种内存模型,在理解的时候应该这样理解:定义了一个二维数组,有3个(5个char)空间的存储变量。
在使用的时候,若要使用中间量操作元素,那么此时中间量应该定义为
char tmp[5] = { 0 };嵌入式物联网需要学的东西真的非常多,千万不要学错了路线和内容,导致工资要不上去!
无偿分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!某鱼上买估计至少要好几十。
点击这里找小助理0元领取:加微信领取资料
如果要打印这个数组,那么可以使用以下函数
nt printAarray(char pArray[][5], int num) { int i = 0; if (pArray == NULL) { return -1; } for (i = 0; i < num; i++) { printf("%s \n", pArray[i]); } return 0; }第三种内存模型char **arr
若有如下定义
char **arr = (char *)malloc(100 * sizeof(char *));//char arr[400] arr[0] = (char *)malloc(100 * sizeof(char));//char buf[100] arr[1] = (char *)malloc(100 * sizeof(char)); arr[2] = (char *)malloc(100 * sizeof(char)); strcpy(arr[0], "abc"); strcpy(arr[1], "def"); strcpy(arr[2], "ghi"); ··· for(int i = 0; i < 3; i++) if(arr[i] != NULL) free(arr[i]); free(arr);这种模型为二级指针的第二种内存模型,在理解的时候应该这样理解:定义了一个二级指针,二级指针就是指向指针的指针,其实就是开辟了100个指针空间,存放了100个地址。这种写法是第一种的简化写法
在使用的时候,若要使用中间量操作元素,那么此时中间量应该定义为
char *tmp = NULL如果要打印这个数组,那么可以使用以下函数
{ int i = 0; if (pArray == NULL) { return -1; } for (i = 0; i < num; i++) { printf("%s \n", pArray[i]); } return 0; }例子
把第一种内存模型的数据排序,运算结果放到第三种内存模型中
#include "stdio.h" #include "string.h" #include "stdlib.h" char **SortArrayAndGen3Mem(const char ** const myArray1, int num, char *str, int *myNum) { char **p = NULL; p= (char **)malloc(num*sizeof(char *)); if (myArray1==NULL || str==NULL|| myNum==NULL) { printf("传入参数错误\n"); p = NULL; goto END; } *myNum = num; for (int i = 0; i < num;i++) { p[i] = NULL; p[i] = (char)malloc(50 * sizeof(char)); memset(p[i], 0, sizeof(p[i])); if (p[i]==NULL) { printf("内存分配错误!\n"); goto END; } strcpy(p[i], myArray1[i]); } char *tmp; for (int i = 0; i < num; i++) { for (int j = i + 1; j < num; j++) { if (strcmp(p[i],p[j])>0) { char *tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } } for (int i = 0; i < num; i++) { printf("%s \n", myArray1[i]); } END: return p; } //释放内存函数 void main() { int i = 0; char **myArray3 = NULL; int num3 = 0; //第一种内存模型 char *myArray[] = {"bbbbb", "aaaaa", "cccccc"}; char *myp = "111111111111"; myArray3 = SortArrayAndGen3Mem(myArray, 3, myp, &num3); for (i=0; i<num3; i++) { printf("%s \n", myArray3[i]); } system("pause"); } #include "stdio.h" #include "stdlib.h" #include "string.h" char **SortArrayAndGet3Mem(const char* const myArray1,int num,char *str,int *myNum); int getArray(char ***newp,int num) ; int freeArray(char ***newpfree,int num); int sortTArray(char *p, int num); void main() { char **myArray3=NULL; int num3=0; char *myArray[]={"bbbb","aaa","cccc"}; char *myp="111111111"; myArray3=SortArrayAndGet3Mem(myArray,3,myp,&num3); system("pause"); } char **SortArrayAndGet3Mem(const char** const myArray1,int num,char *str,int *myNum) { int ret=0; char **p=NULL; int i=0; char **p1=NULL; p1=(char **)myArray1; ret=getArray(&p,num +1); for (i=0;i<num;i++) { strcpy(p[i],p1[i]); } strcpy(p[i], str); ret=sortTArray(p,num +1); for (i=0;i<num +1;i++) { printf("%s\n",p[i]); } ret=freeArray(&p,num +1); *myNum = num +1; return p; } int getArray(char ***newp,int num) { int i=0; int ret=0; char **tmp = NULL; tmp = (char **)malloc(num*sizeof(char *)); for (i=0;i<num;i++) { tmp[i]=(char*)malloc(sizeof(char)*100); } *newp = tmp; // return 0; } // int freeArray(char ***newpfree,int num) { char **p=NULL; int i=0; int ret=0; p=*newpfree; for (i=0;i<num;i++) { free(p[i]); } free(p); *newpfree = NULL; // return ret; } //int sortTArray(char ***Arraystr, int num) int sortTArray(char **Arraystr, int num) { int i , j = 0; for (i=0; i<num; i++) { for (j=i+1; j<num; j++) { if (strcmp((Arraystr)[i],(Arraystr)[j])>0) { char tmp[100]; strcpy(tmp,(Arraystr)[i]); strcpy((Arraystr)[i],(Arraystr)[j]); strcpy((Arraystr)[j],tmp); } } } for (i=0;i<num;i++) { printf("%s\n",(Arraystr)[i]); } return 0; } 原文链接:https://mp.weixin.qq.com/s/LRMjzIYIVPs6x6ja5ffp_w
转载自:嵌入式大杂烩
原文链接:详解C语言二级指针三种内存模型
本文来源网络,免费传达知识,版权归原作者所有。如涉及作品版权问题,请联系我进行删除。
版权声明:
作者: FreeClashNode
链接: https://www.freeclashnode.com/news/article-2573.htm
来源: www.freeclashnode.com
文章版权归作者所有,未经允许请勿转载。
热门文章
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 10月11日免费节点分享|18.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月13日免费节点分享|22.1M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月9日免费节点分享|22.3M/S,Singbox节点/Clash节点/Shadowrocket节点/V2ray节点|免费上网梯子每天更新
- 10月12日免费节点分享|22.7M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月10日免费节点分享|18.3M/S,V2ray节点/Singbox节点/Clash节点/SSR节点|免费上网梯子每天更新
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
最新文章
- 当Vmess突然失效:一场技术与封锁的无声博弈
- 11月7日免费节点分享|22.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 梅林固件科学上网完全指南:从入门到精通,零成本解锁网络自由
- 11月6日免费节点分享|20.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 幸运破解器科学上网全解析:解锁数字世界的终极指南
- 11月5日免费节点分享|21.9M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 11月4日免费节点分享|18M/S,Clash节点/Singbox节点/SSR节点/V2ray节点|免费上网梯子每天更新
- 穿越数字迷雾:深度解析V2Ray中转迟滞之谜与极速优化指南
- 跨越数字边界:科学上网的终极指南与深度思考
- 从连接到掌控:深入理解 Quantumult 的工作流程与网络管理哲学
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度解析《荒野冲突》:征服与策略的终极对决
- 11月1日免费节点分享|20.3M/S,V2ray节点/Shadowrocket节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 斐讯路由器刷机配置v2ray全攻略:打造安全高效的翻墙网络环境
- 10月31日免费节点分享|18.7M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 谷歌浏览器科学上网扩展全攻略:从入门到精通
- 10月30日免费节点分享|20.4M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 苹果手机V2Ray完全指南:从零开始打造安全上网环境
- 10月29日免费节点分享|21.6M/S,SSR节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 探索数字世界的门户:电脑科学上网贴吧完全指南
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 穿越数字边界:科学上网的全面指南与深度思考
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 数字世界的破壁之旅:科学上网全指南与实用工具深度解析
- 10月26日免费节点分享|21.5M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 跨越知识边界:浙大学子的全球学术网络探索指南
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月24日免费节点分享|20.5M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 刘大猫科学上网全攻略:解锁全球网络的终极指南
- 10月23日免费节点分享|22.2M/S,Shadowrocket节点/Clash节点/Singbox节点/V2ray节点|免费上网梯子每天更新
- 全面解析Quantumult安卓版:功能、安装与配置教程
- 双重防护,畅享无界:AdGuard联手V2RayNG,打造极致安全的上网体验
- 10月22日免费节点分享|18.8M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 掌握Clash白名单设置:精准控制代理流量的艺术
- 10月21日免费节点分享|22M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 突破网络边界:技术科学上网的终极安全指南
- 10月20日免费节点分享|22.9M/S,SSR节点/V2ray节点/Clash节点/Singbox节点|免费上网梯子每天更新
- Shadowrocket 选购指南:从入门到精通,找到你的专属代理神器
- 10月19日免费节点分享|19.3M/S,Singbox节点/V2ray节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 深入解读:V2Ray为何比SSR更安全?一场网络隐私的全方位较量
- 10月18日免费节点分享|20.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度指南:Shadowrocket从入门到精通——iOS用户的网络自由密钥
- 10月17日免费节点分享|18M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 深入解析科学上网套:全方位使用指南与常见问题解答
- 10月16日免费节点分享|22M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月15日免费节点分享|21.2M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 全局掌控:安卓设备上 Clash 全局模式完整设置指南与深度解析
- 解锁全球游戏体验:育碧平台科学上网全攻略与深度解析
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 华为Mate 10科学上网全攻略:突破限制,畅游全球网络
归档
- 2026-11 13
- 2026-10 61
- 2026-09 58
- 2026-08 63
- 2026-07 61
- 2026-06 55
- 2026-05 56
- 2026-04 51
- 2026-03 60
- 2026-02 52
- 2026-01 56
- 2025-12 59
- 2025-11 55
- 2025-10 56
- 2025-09 55
- 2025-08 49
- 2025-07 31
- 2025-06 30
- 2025-05 31
- 2025-04 30
- 2025-03 388
- 2025-02 360
- 2025-01 403
- 2024-12 403
- 2024-11 390
- 2024-10 403
- 2024-09 388
- 2024-08 402
- 2024-07 427
- 2024-06 442
- 2024-05 181
- 2024-04 33
- 2024-03 31
- 2024-02 29
- 2024-01 51
- 2023-12 52
- 2023-11 32
- 2023-10 32
- 2023-09 3