您的位置:首页 > 其它

二级指针的3种内存模型

2016-09-14 15:32 267 查看
1.二级指针第一种内存模型

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>

int sort_arr(char**myArr,int num)
{
char*temp;
char **p = myArr;
int i,j;

for (i=0;i<num;i++)
{
for (j=i+1;j<num;j++)
{
if (strcmp(p[i],p[j])>0)
{
temp = p[i];//主意我们交换的只是指针的值,改变指针的指向
p[i] = p[j];
p[j] = temp;
}
}
}
return 0;
}
int print_arr(char**myArr,int num)
{
int i = 0;

for (i=0;i<num;i++)
{
printf("num:%s\n",*(myArr+i));
}
return 0;
}
int main()
{
//数组 数组中的每一个元素是指针,指针数组
//1.排序 2.指针做函数参数 3.内存
int num,i,j;
char*temp;
char* myArr[4]={"ddd","cccc","bbbb","aaaaaaa"};
print_arr(myArr,4);
sort_arr(myArr,4);
printf("----------\n");
print_arr(myArr,4);
system("pause");
return 0;
}


2.二级指针第二种内存模型

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>

int sort_arr(char myArr[10][20],int num)
{
char temBuf[20];
//char **p = myArr;
int i,j;

for (i=0;i<num;i++)
{
for (j=i+1;j<num;j++)
{
if (strcmp(myArr[i],myArr[j])>0)
{
strcpy(temBuf , myArr[i]);//把数据交换 内存块
strcpy(myArr[i] , myArr[j]);
strcpy(myArr[j] , temBuf);
}
}
}
return 0;
}
/*
//第一种内存模型打印函数不适用第二种内存模型
//原因:二级指针做输入_第一种内存模型 myArr + 1 与第二种内存模型 myArr + 1
//指针步长不一样哈 指针所指向内存空间的数据不一样
int print_arr(char**myArr,int num)
{
int i = 0;

for (i=0;i<num;i++)
{
printf("num:%s\n",*(myArr+i));
}
return 0;
}*/

int print_arr(char myArr[10][20],int num)
{
int i = 0;

for (i=0;i<num;i++)
{
printf("num:%s\n",*(myArr+i));
}
return 0;
}

int main()
{
//数组 数组中的每一个元素是指针,指针数组
//1.排序 2.指针做函数参数 3.内存
char temBuf[30];
char myArr[10][20]={"ff","bbbbb","ddddd","ccccc","eeeee"};
int num = 5;
print_arr(myArr,num);

sort_arr(myArr,num);

printf("---------\n");
print_arr(myArr,num);

system("pause");
return 0;
}


3.二级指针第三种内存模型

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>

//在堆上分配内存空间 通过return返回
char** getMen(int num)
{
    char **p = NULL;
    int i,j;
    p = (char **)malloc(sizeof(char*)*num);

    for (i =0;i<num;i++)
    {
        p[i] = (char*)malloc(sizeof(char)*100);
        sprintf(p[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据
    }
    return p;
}

//在堆上分配内存空间,通过函数参数返回
int getMen2(char ***p,int num)
{
    char **tmp = NULL;
    int i,j;
    tmp = (char **)malloc(sizeof(char*)*num);
    if (p==NULL)
    {
        return -1;
    }
    for (i =0;i<num;i++)
    {
        tmp[i] = (char*)malloc(sizeof(char)*100);
        sprintf(tmp[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据
    }
    *p = tmp;//  int a ; int *p ; p = &a;   *p=10  ;通过三级指针修改二级指针的值
    return 0;
}
//释放内存空间
int freeMen(char**p,int num)
{
    int ret =0,i;
    for (i =0;i<num;i++)
    {
        if (p[i]!=NULL)
        {
            free(p[i]);
            p[i]=NULL;
        }
    }
    if (p!=NULL)
    {
        free(p);
        p=NULL;
    }
    return ret;
}
//打印
int print_arr(char**myArr,int num)
{
    int i = 0;

    for (i=0;i<num;i++)
    {
        printf("num:%s\n",*(myArr+i));
    }
    return 0;
}
//排序 只交换指针
int sort_arr(char**myArr,int num)
{
    char*temp;
    char **p = myArr;
    int i,j;

    for (i=0;i<num;i++)
    {
        for (j=i+1;j<num;j++)
        {
            if (strcmp(p[i],p[j])>0)
            {
                temp = p[i];//主意我们交换的只是指针的值,改变指针的指向
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
    return 0;
}
//排序2 交换数据
int sort_arr2(char**myArr,int num)
{
    char temBuf[20];
    char **p = myArr;
    int i,j;

    for (i=0;i<num;i++)
    {
        for (j=i+1;j<num;j++)
        {
            if (strcmp(p[i],p[j])>0)
            {
                strcpy(temBuf,p[i]);//交换数据
                strcpy(p[i] , p[j]);
                strcpy(p[j] , temBuf);
            }
        }
    }
    return 0;
}
int main()
{
    //1.排序 2.指针做函数参数 3.内存
    char **p = NULL;

    //p = getMen(5);
    getMen2(&p,5);

    //排序前打印
    print_arr(p,5);
    //排序
    sort_arr2(p,5);
    //排序后打印
    print_arr(p,5);
    //释放内存
    freeMen(p,5);
    p=NULL;
    system("pause");
    return 0;
}

4.二级指针三种内存模型图:



5.二级指针强化练习(两个辅助指针变量挖字符串)

1)使用第二种内存模型 主调函数分配内存

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
/*函数功能 根据字符c来分割字符串str*/
int splitStr(const char *str,char c ,char buf[10][20],int *num)
{
char* p = NULL,*pTmp = NULL;//两个辅助指针变量
int tmpcount = 0,len;
p = str;
pTmp = str;
do
{
p = strchr(p,c);

if (p!=NULL)
{
if (p-pTmp>0)
{
strncpy(buf[tmpcount],pTmp,p-pTmp);
buf[tmpcount][p-pTmp] = '\0';
//printf("%s\n",buf[tmpcount]);
tmpcount++;
pTmp = p = p+1;
len = strlen(p);//用来保存最后一个字符串的长度
}
}
else
{
//拷贝最后一个分割的字符串包括\0
strncpy(buf[tmpcount],pTmp,len+1);
break;
}

} while (*p!='\0');
*num=tmpcount+1;
return 0;
}
/*函数功能 打印二维数组*/

void printArr(char a[10][20],int n)
{
int i;
for (i=0;i<n;i++)
{
printf("%s\n",*(a+i));
}
}

int main()
{
char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
char ctemp = ',';
char myArr[10][20]={0};
char *tmp;

int ret;
int n,i;

ret = splitStr(input,ctemp,myArr,&n);
if (ret!=0)
{
printf("error\n");
}
printArr(myArr,n);

system("pause");
return 0;
}

2)使用第三种内存模型 主调函数分配内存

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>

/*第三种内存模型*/
int splitStr(const char *str,char c,char **buf,int *count)
{

char* p = NULL,*pTmp = NULL;//两个辅助指针变量
int tmpcount = 0,len;
p = str;
pTmp = str;
do
{
p = strchr(p,c);

if (p!=NULL)
{
if (p-pTmp>0)
{
strncpy(buf[tmpcount],pTmp,p-pTmp);
buf[tmpcount][p-pTmp] = '\0';
//printf("%s\n",buf[tmpcount]);
tmpcount++;
pTmp = p = p+1;
len = strlen(p);//用来保存最后一个字符串的长度
}
}
else
{
//拷贝最后一个分割的字符串包括\0
strncpy(buf[tmpcount],pTmp,len+1);
break;
}
} while (*p!='\0');
*count=tmpcount+1;
return 0;
}

int main()
{
char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
char ctemp = ',';
char **p = NULL;
int ret;
int n,i;
p=(char**)malloc(10*sizeof(char*));
if (p==NULL)
{
return;
}
for (i=0;i<10;i++)
{
p[i] = (char *)malloc(20*sizeof(char));
}
ret = splitStr(input,ctemp,p,&n);
if (ret!=0)
{
printf("error\n");
}
for (i=0;i<n;i++)
{
printf("%s\n",*(p+i));
}
//释放内存
system("pause");
return 0;
}

3)使用第三种内存模型 被调函数分配内存 通过return返回

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
/*第三种内存模型 被调函数分配内存 通过return返回*/
char** splitStr2(char *str,char c,int *count)
{
char* p = NULL,*pTmp = NULL;//两个辅助指针变量
int tmpcount = 0,len,len2;
char**buf;
p = str;
pTmp = str;

//第一遍扫描 开辟第一维空间
do
{
p = strchr(p,c);
if (p!=NULL)
{
if (p-pTmp>0)
{
tmpcount++;
pTmp = p = p+1;
}
}
else
{
break;
}
} while (*p!='\0');
*count=tmpcount+1;
buf = (char**)malloc((tmpcount+1)*sizeof(char*));
//printf("tmpcount:%d\n",tmpcount);
if(buf==NULL)
{
return NULL;
}

//第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串
tmpcount = 0;
p = str;
pTmp = str;
do
{
p = strchr(p,c);
if (p!=NULL)
{
if (p-pTmp>0)
{
len = p-pTmp+1;
buf[tmpcount] = (char*)malloc(len*sizeof(char));
if (buf[tmpcount] == NULL)
{
free(buf);
return ;
}
strncpy(buf[tmpcount],pTmp,p-pTmp);
buf[tmpcount][p-pTmp] = '\0';
tmpcount++;
pTmp = p = p+1;
len2 = strlen(p);//用来保存最后一个字符串的长度
}
}
else
{
//printf("tmpcount:%d\n",tmpcount);
//printf("len2:%d\n",len2);
buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));
if (buf[tmpcount] == NULL)
{
free(buf);
return ;
}
//拷贝最后一个分割的字符串包括\0
strncpy(buf[tmpcount],pTmp,len2+1);
break;
}
} while (*p!='\0');
return buf;
}

int main()
{
char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
char ctemp = ',';
char **p = NULL;
int ret;
int n,i;
p = splitStr2(input,ctemp,&n);
printf("n:%d\n",n);
for (i=0;i<n;i++)
{
printf("%s\n",*(p+i));

}
     //释放内存空间
     system("pause");
return 0;
}


4)使用第三种内存模型 被调函数分配内存 通过函数参数返回

<pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
/*第三种内存模型 被调函数分配内存 通过tmpbuf返回*/
 int splitStr3(char *str,char c,char*** tmpbuf,int *count)
{
    char* p = NULL,*pTmp = NULL;//两个辅助指针变量
    int tmpcount = 0,len,len2;
    char**buf;
    p = str;
    pTmp = str;

    //第一遍扫描 开辟第一维空间
    do
    {
        p = strchr(p,c);
        if (p!=NULL)
        {
            if (p-pTmp>0)
            {    
                tmpcount++;
                pTmp = p = p+1;
            }    
        }
        else
        {
            break;
        }    
    } while (*p!='\0');
    *count=tmpcount+1;
    buf = (char**)malloc((tmpcount+1)*sizeof(char*));
    //printf("tmpcount:%d\n",tmpcount);
    if(buf==NULL)
    {
        return -1;
    }

    //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串
    tmpcount = 0;
    p = str;
    pTmp = str;
    do
    {
        p = strchr(p,c);
        if (p!=NULL)
        {
            if (p-pTmp>0)
            {    
                len = p-pTmp+1;
                buf[tmpcount] = (char*)malloc(len*sizeof(char));
                if (buf[tmpcount] == NULL)
                {
                    free(buf);
                    return -2;
                }
                strncpy(buf[tmpcount],pTmp,p-pTmp);
                buf[tmpcount][p-pTmp] = '\0';
                tmpcount++;
                pTmp = p = p+1;
                len2 = strlen(p);//用来保存最后一个字符串的长度
            }    
        }
        else
        {
            //printf("tmpcount:%d\n",tmpcount);
            //printf("len2:%d\n",len2);
            buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));
            if (buf[tmpcount] == NULL)
            {
                free(buf);
                return -2;
            }
            //拷贝最后一个分割的字符串包括\0
            strncpy(buf[tmpcount],pTmp,len2+1);
            break;
        }    
    } while (*p!='\0');
    *tmpbuf = buf;
    return 0;
}
 //释放内存空间
 void freeMem(char**p,int count)
 {
     int i =0;
     if (p == NULL)
     {
         return ;
     }
     for (i=0;i<count;i++)
     {
         if (p[i]!=NULL)
         {
             free(p[i]);
         }
     }
     if (p!=NULL)
     {
         free(p);
     }
 }
int main()
{
    char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
    char ctemp = ',';
    char **p = NULL;
    int ret;
    int n,i;
    ret = splitStr3(input,ctemp,&p,&n);
    if (ret!= 0)
    {
        printf("error");
        return -1;
    }
    //printf("n:%d\n",n);
    for (i=0;i<n;i++)
    {
        printf("%s\n",*(p+i));
    
    }
    //释放内存
    freeMem(p,n);
    system("pause");
    return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: