美文网首页
二级指针的第三种内存模型

二级指针的第三种内存模型

作者: 带着白卡去旅行 | 来源:发表于2018-09-12 18:33 被阅读15次

    no.1交换指针的方式排序:

    tmp临时字符串指针
    p2[i] = (char
    )malloc(sizeof(char)*100);//char buf[100]
    通过二级指针修改一级指针的内容

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

    内存四区图


    内存四区图
    
    #define  _CRT_SECURE_NO_WARNINGS
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    
    
    void main()
    {
        int i = 0, j = 0;
        char *tmp = NULL;
        char **p2 = NULL;
        int num = 5;
        p2 = (char**)malloc(sizeof(char)*num);
    
        for (i=0;i<num;i++)
        {
    
            p2[i] = (char*)malloc(sizeof(char*)*100);//char buf[100]
            sprintf(p2[i], "%d%d%d", i + 1, i + 1, i + 1);
        }
    
        //排序前:
        printf("排序前\n");
        for (int i=0;i<num;i++)
        {
            printf("%s\n",p2[i]);
        }
    
        //排序后:
        
        for (int 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("排序后\n");
        for ( i = 0; i < num; i++)
        {
            printf("%s\n", p2[i]);
        }
    
    
        //释放内存
        for (i=0;i<num;i++)
        {
            if (p2[i]!=NULL)
            {
                free(p2[i]);
                p2[i] = NULL;
            }
    
        }
        if (p2!=NULL)
        {
            free(p2);
        }
        printf("hell world\n");
        system("pause");
        return 0;
    }
    

    交换内存块

    char tmpbuf[100];
    for (int i = 0; i < num; i++)
        {
            for (j = i + 1; j < num; j++)
            {
                if (strcmp(p2[i], p2[j]) < 0)
                {
                    strcmp(tmpbuf , p2[i]);
                    strcmp(p2[i] , p2[j]);
                    strcmp(p2[j] , tmpbuf);
    
                }
            }
        }
    

    可以实现相同的效果。

    封装效果[指针做函数参数]

    
    #define  _CRT_SECURE_NO_WARNINGS
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    
    
    //分配内存【二级指针】
    char**getMem(int num)
    {
        char**p2 = NULL;
        p2 = (char**)malloc(sizeof(char*)*num);
        if (p2 == NULL)
        {
            return NULL;
    
        }
    
        for (int i=0;i<num;i++)
        {
    
            p2[i] = (char*)malloc(sizeof(char)*100);//char buf[100]
            sprintf(p2[i], "%d%d%d", i + 1, i + 1, i + 1);
        }
        return p2;//返回p2首地址
    }
    
    void printMyArray(char** myArray, int num)
    {
        int i = 0;
        for (i = 0; i < num; i++)
    
        {
            printf("%s\n", *(myArray + i));
        }
    }
    void sortMyArray(char**myArray,int num)
    {
        int i = 0; int j = 0;
        char *tmp=NULL;
        for (int i=0;i<num;i++)
        {
            for (int j=i;j<num;j++)
            {
                if (strcmp(myArray[i],myArray[j])>0)
                {
                    tmp = myArray[i];
                    myArray[i] = myArray[j];
                    myArray[j] = tmp;
                }
            }
        }
    }
    
    
    void main()
    {
        int i = 0, j = 0;
        char *tmp = NULL;
        char **p2 = NULL;
        int num = 5;
        p2 = getMem(num);
    
        //排序前:
    
        printf("排序前\n");
        printMyArray(p2,num);
        //排序后:
        sortMyArray(p2, num);
    
        //排序后:
        printf("排序后\n");
        printMyArray(p2, num);
        //释放内存
        for (i=0;i<num;i++)
        {
            if (p2[i]!=NULL)
            {
                free(p2[i]);
                p2[i] = NULL;
            }
    
        }
        if (p2!=NULL)
        {
            free(p2);
        }
        printf("hell world\n");
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:二级指针的第三种内存模型

          本文链接:https://www.haomeiwen.com/subject/zylbgftx.html