C 二级指针和三级指针的使用
来源:互联网 发布:rrt算法 编辑:程序博客网 时间:2024/05/01 08:11
//这里给出三级指针作为参数和二级指针作为参数的demo
//三级指正作为参数,内存分配在函数内
//二级指针作为参数,内存分配在函数外,也就是调用的地方
#include <string.h>#include <stdlib.h>#include <stdio.h>#include <iostream>#include <string>#include <vector>using namespace std;void func(char*** args, int &c){ int count = 3; c = count; const char *iface[] = {"getStatus", "setStatus", "getUserName"}; *args = (char**)malloc(count*sizeof(char*)); for( int i = 0; i < count; i++ ) { cout << iface[i] << endl; int len = strlen(iface[i])+1; (*args)[i] = (char*)malloc(len*sizeof(char)); memset((*args)[i], 0, len); strcpy((*args)[i], iface[i]); }}void func2(char**args, int &num, int* lens){ int count = 3; const char *iface[] = {"getStatus", "setStatus", "getUserName"}; num = count; for( int i = 0; i < count; i++ ) { lens[i] = strlen(iface[i]); } if( args == NULL ) return; for( int i = 0; i < count; i++ ) { strcpy(args[i], iface[i]); }}int main(){ char **array = NULL; int count = -1; func(&array, count); vector<string> ifaceList; cout << "count = " << count << endl; for( int i = 0; i < count; i++ ) { cout << array[i] << endl; ifaceList.push_back(array[i]); if( array[i] != NULL ) { free(array[i]); array[i] = NULL; } } if( array != NULL ) { free(array); array = NULL; } cout << "----------------" << endl; for( vector<string>::iterator iter = ifaceList.begin(); iter != ifaceList.end(); iter++ ) { cout << *iter << endl; } cout << "======= who allocate who free ==========" << endl; char** pIfaceList = NULL; int num = -1; int lens[100] = {-1}; func2(pIfaceList, num, lens); cout << "num = " << num << endl; for( int i = 0; i < num; i++ ) { cout << "len[" << i << "] = " << lens[i] << endl; } //allocate memory pIfaceList = (char**)malloc(num*sizeof(char*)); for(int i = 0 ; i < num; i++) { int nLen = lens[i]+1; pIfaceList[i] = (char*)malloc(sizeof(char)*nLen); memset(pIfaceList[i], 0, sizeof(char)*nLen); } func2(pIfaceList, num, lens); vector<string> ifaceList2; for( int i = 0; i < num; i++ ) { cout << "pIfaceList[" << i << "] = " << pIfaceList[i] << endl; ifaceList2.push_back(pIfaceList[i]); free(pIfaceList[i]); pIfaceList[i] = NULL; } if( pIfaceList != NULL ) { free(pIfaceList); pIfaceList = NULL; } return 0;}
0 0
- C 二级指针和三级指针的使用
- 经典笔试题:一级指针数组、二级指针数组和三级指针的联合使用详解
- C语言:一级指针,二级指针,三级指针,指针数组,数组指针的综合示例
- C语言二级指针的介绍和使用
- 指针的使用二级指针
- 二级指针的使用
- 二级指针的使用
- 二级指针的使用
- 二级指针的使用
- C语言二级指针与指针数组的结合使用.
- const 和二级指针的使用
- c语言二级指针和函数指针
- C语言指针和二级指针
- 【总结归纳】一级指针和二级指针的使用
- 指针和二级指针的区别
- 二级指针的使用实例
- 链表:二级指针的使用
- 链表中二级指针的使用
- 两种方法求解逆序对
- java内存区域理解-初步了解
- 三种静态查找的思路及具体实现
- javascript笔试题(3)
- Ceph剖析:数据分布之CRUSH算法与一致性Hash
- C 二级指针和三级指针的使用
- python 中 list 列表 的十种操作方法:添加,插入,弹出,删除,延长,运算,查找,排序,反转,采用递归函数深度遍历list
- 题目34
- MySQL中的一些常用的函数
- BZOJ3427: Poi2013 Bytecomputer
- Hdu-5289 Assignment (二分+RMQ || 单调队列)
- 题目50
- min与windef.h
- 极猫流量邀请码 10183831