二级指针内存模型

Posted wanghao-boke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二级指针内存模型相关的知识,希望对你有一定的参考价值。

自定义内存,使用堆中分配内存来控制数据交换

 

交换指针指向的内容

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

int main()
{
    char **p2 = NULL;
    int i,j;
    int num = 5;
    p2 = (char **)malloc(sizeof(char *) * num);

    for(i = 0; i < num; i++)
    {
        p2[i] = (char *)malloc(sizeof(char) * 100);
        sprintf(p2[i],"%d%d%d",i+1,i+1,i+1);
    }        

    //print
    printf("before sort
");
    for(i = 0; i < num; i++)
    {
        printf("%s 
",p2[i]);
    }

    char *tmp = NULL;
    //sort
    for(i = 0; i < num; i++)
    {
        for(j = i+1; j < num; j++)
        {
            if(strcmp(p2[i],p2[j]) < 0)
            {
                tmp = p2[i];
                p2[i] = p2[j];
                p2[j] = tmp;
            }
        }
    }

    printf("after sort
");
    for(i = 0; i < num; i++)
    {
        printf("%s 
",p2[i]);
    }

    //free
    for(i = 0; i < num; i++)
    {
        if(p2[i] != NULL)
        {
            free(p2[i]);
            p2[i] = NULL;
        }
    }

    if(p2 != NULL)
    {
        free(p2);
    }
    return 0;
}

交换指针指向的内存块

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

int main()
{
    char **p2 = NULL;
    int i,j;
    int num = 5;
    char tmpbuf[100];
    p2 = (char **)malloc(sizeof(char *) * num);

    for(i = 0; i < num; i++)
    {
        p2[i] = (char *)malloc(sizeof(char) * 100);
        sprintf(p2[i],"%d%d%d",i+1,i+1,i+1);
    }        

    //print
    printf("before sort
");
    for(i = 0; i < num; i++)
    {
        printf("%s 
",p2[i]);
    }

    char *tmp = NULL;
    //sort change the storage
    for(i = 0; i < num; i++)
    {
        for(j = i+1; j < num; j++)
        {
            if(strcmp(p2[i],p2[j]) < 0)
            {
                strcpy(tmpbuf,p2[i]);
                strcpy(p2[i],p2[j]);
                strcpy(p2[j],tmpbuf);
            }
        }
    }

    printf("after sort
");
    for(i = 0; i < num; i++)
    {
        printf("%s 
",p2[i]);
    }

    //free
    for(i = 0; i < num; i++)
    {
        if(p2[i] != NULL)
        {
            free(p2[i]);
            p2[i] = NULL;
        }
    }

    if(p2 != NULL)
    {
        free(p2);
    }
    return 0;
}

函数封装接口

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

char **getMem(int num)
{
    char **p2 = NULL;
    int i;
    p2 = (char **)malloc(sizeof(char *) * num);
    if(p2 == NULL)
    {
        return NULL;
    }

    for(i = 0; i < num; i++)
    {
        p2[i] = (char *)malloc(sizeof(char) * 100);
        sprintf(p2[i],"%d%d%d",i+1,i+1,i+1);
    }        

    return p2;
}

void sortMyarray(char **myArray,int num)
{
     int i = 0,j = 0;
    char *tmp = NULL;
    //sort change the storage
    for(i = 0; i < num; i++)
    {
        for(j = i; j < num; j++)
        {
            if(strcmp(myArray[i],myArray[j]) < 0)
            {
                tmp = myArray[i];
                myArray[i] = myArray[j];
                myArray[j] = tmp;
            }
        }
    }
}

void printMyArray(char **myArray,int num)
{
    int i = 0;
    for(i = 0; i < num; i++)
    {
        printf("%s 
",*(myArray+i));
    }
}

void getMem_free(char **p2,int num)
{
    int i;
    for(i = 0; i < num; i++)
    {
        if(p2[i] != NULL)
        {
            free(p2[i]);
            p2[i] = NULL;
        }
    }

    if(p2 != NULL)
    {
        free(p2);
    }    
}

int main()
{
    char **p2 = NULL;
    int i,j;
    int num = 5;
    char tmpbuf[100];

    p2 = getMem(num);
    //print
    printf("before sort
");
    printMyArray(p2,num);

    sortMyarray(p2,num);

    printf("after sort
");
    printMyArray(p2,num);

    //free
    getMem_free(p2,num);

    return 0;
}

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

int main()

{

       char **p2 = NULL;

       int i,j;

       int num = 5;

       p2 = (char **)malloc(sizeof(char *) * num);

 

       for(i = 0; i < num; i++)

       {

              p2[i] = (char *)malloc(sizeof(char) * 100);

              sprintf(p2[i],"%d%d%d",i+1,i+1,i+1);

       }            

 

       //print

       printf("before sort ");

       for(i = 0; i < num; i++)

       {

              printf("%s ",p2[i]);

       }

 

       char *tmp = NULL;

       //sort

       for(i = 0; i < num; i++)

       {

              for(j = i+1; j < num; j++)

              {

                     if(strcmp(p2[i],p2[j]) < 0)

                     {

                            tmp = p2[i];

                            p2[i] = p2[j];

                            p2[j] = tmp;

                     }

              }

       }

 

       printf("after sort ");

       for(i = 0; i < num; i++)

       {

              printf("%s ",p2[i]);

       }

 

       //free

       for(i = 0; i < num; i++)

       {

              if(p2[i] != NULL)

              {

                     free(p2[i]);

                     p2[i] = NULL;

              }

       }

 

       if(p2 != NULL)

       {

              free(p2);

       }

       return 0;

}

 

以上是关于二级指针内存模型的主要内容,如果未能解决你的问题,请参考以下文章

C 语言二级指针 内存模型图 ( 指针数组 | 二维数组 | 自定义二级指针内存 )

C语言 二级指针内存模型

二级指针的三种内存模型

大数据内存模型(二级指针)

二级指针内存模型

二级指针内存模型