zoukankan      html  css  js  c++  java
  • 二级指针三种内存模型综合(把第1种内存模型和第2种内存模型数据copy到第3种内存模型)

    #define _CRT_SECURE_NO_WARNINGS
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    //char **p 二级指针    char (*p)[30] 数组指针   char ***p3手动创建内存模型,在被调函数sort中分配内存
    int copyandsort(char **myp1, int num1, char(*myp2)[30], int num2, char ***myp3, int *num3)
    {
        int i = 0, j = 0, k = 0;
        int tmplen = 0;
        char **p3 = NULL;//定义个辅助指针变量,把实参二级指针接过来
    
        //malloc多大空间内存呢?足够容纳myp指向的内存大小加myp2指向的内存大小就可以了
        //myp共num1个char*    myp2共num2个char* 
        p3 = (char **)malloc((num1 + num2) * sizeof(char*));
        if (p3 == NULL)//如果malloc失败
        {
            return -1;
        }
    
        //根据第一块内存的大小申请内存  根据字符串长度分配内存
        for (i = 0; i<num1; i++)
        {
            tmplen = strlen(myp1[i]) + 1;//加的1是''占1位 +  strlen(myp1[i]) 第i个字符串的长度
            p3[i] = (char *)malloc(tmplen * sizeof(char));
            if (p3[i] == NULL)//申请失败
            {
                return -2;
            }
            strcpy(p3[i], myp1[i]);//申请内存后copy
        }
    
        //根据第二块内存的大小申请
        for (j = 0; j<num2; j++, i++)//???
        {
            tmplen = strlen(myp2[j]) + 1;//加的1是''占1位
            p3[i] = (char *)malloc(tmplen * sizeof(char));
            if (p3[i] == NULL)//申请失败
            {
                return -3;
            }
            strcpy(p3[i], myp2[j]);
        }
    
    
    
        //排序 可以交换指针指向的地址,也可以交换指针指向内存空间的内容
        //这儿改变指针的指向来实现
        tmplen = num1 + num2;
        char *tmpP = NULL;
        for (i = 0; i< tmplen; i++)
        {
            for (j = i+1; j<tmplen; j++)
            {
                if (strcmp(p3[i], p3[j]) > 0)//myp3[i]>myp3[j]
                {
                    tmpP = p3[i];
                    p3[i] = p3[j];
                    p3[j] = tmpP;
                }
            }
        }
    
        //间接赋值
        *num3 = num1 + num2;
        *myp3 = p3;
    
        return 0;
    }
    
    //释放二级指针
    void sortFree(char **myp, int len)
    {
        if (myp == NULL)
        {
            return;
        }
        int i = 0;
        for (i = 0; i < len; i++)
        {
            free(myp[i]);
        }
        free(myp);   
    }
    
    //既释放指针,又把指针置为NULL,防止野指针
    void sortFree2(char ***myp, int len)
    {
        int i = 0;
        char **p = NULL;//定义个辅助指针变量,把二级指针实参接过来
        if (myp == NULL)
        {
            return;
        }
    
        p = *myp;//还原成二级指针,即要修改的实参指针
        if (p == NULL)
        {
            return;
        }
    
        for (i = 0; i < len; i++)
        {
            free(myp[i]);
        }
        free(myp);
        *myp = NULL;
    }
    
    //把二级指针第1种内存模型(p1指向的地址)
    //和第2种内存模型(buf2指向的地址)的数据
    //copy到第3种内存模型(p3指向的地址)
    int main()
    { 
        char *p1[] = { "aaaaaaaaa", "ccccc", "bbb" };
        char buf2[10][30] = { "111", "333", "22222" };
        char **p3 = NULL;
        int len1, len2, len3;
        int i = 0;
        len1 = sizeof(p1) / sizeof(*p1);//p1是指针的数组,*p1就是数组首元素p1[0],即第一个char*指针(4字节)
        len2 = 3;
    
        int ret = 0;
        ret = copyandsort(p1, len1, buf2, len2, &p3, &len3);
        if (ret != 0)
        {
            printf("fun sort() error: %d 
    ", ret);
            return ret;
        }
    
        for (i = 0; i<len3; i++ )
        {
            printf("%s 
    ", p3[i]);
        }
    
        system("pause");
        return ret;
    }

    #define _CRT_SECURE_NO_WARNINGS#include <string.h>#include <stdio.h>#include <stdlib.h>
    //char **p 二级指针    char (*p)[30] 数组指针   char ***p3手动创建内存模型,在被调函数sort中分配内存int copyandsort(char **myp1, int num1, char(*myp2)[30], int num2, char ***myp3, int *num3){int i = 0, j = 0, k = 0;int tmplen = 0;char **p3 = NULL;//定义个辅助指针变量,把实参二级指针接过来
    //malloc多大空间内存呢?足够容纳myp指向的内存大小加myp2指向的内存大小就可以了//myp共num1个char*    myp2共num2个char* p3 = (char **)malloc((num1 + num2) * sizeof(char*));if (p3 == NULL)//如果malloc失败{return -1;}
    //根据第一块内存的大小申请内存  根据字符串长度分配内存for (i = 0; i<num1; i++){tmplen = strlen(myp1[i]) + 1;//加的1是''占1位 +  strlen(myp1[i]) 第i个字符串的长度p3[i] = (char *)malloc(tmplen * sizeof(char));if (p3[i] == NULL)//申请失败{return -2;}strcpy(p3[i], myp1[i]);//申请内存后copy}
    //根据第二块内存的大小申请for (j = 0; j<num2; j++, i++)//???{tmplen = strlen(myp2[j]) + 1;//加的1是''占1位p3[i] = (char *)malloc(tmplen * sizeof(char));if (p3[i] == NULL)//申请失败{return -3;}strcpy(p3[i], myp2[j]);}


    //排序 可以交换指针指向的地址,也可以交换指针指向内存空间的内容//这儿改变指针的指向来实现tmplen = num1 + num2;char *tmpP = NULL;for (i = 0; i< tmplen; i++){for (j = i+1; j<tmplen; j++){if (strcmp(p3[i], p3[j]) > 0)//myp3[i]>myp3[j]{tmpP = p3[i];p3[i] = p3[j];p3[j] = tmpP;}}}
    //间接赋值*num3 = num1 + num2;*myp3 = p3;
    return 0;}
    //释放二级指针void sortFree(char **myp, int len){if (myp == NULL){return;}int i = 0;for (i = 0; i < len; i++){free(myp[i]);}free(myp);   }
    //既释放指针,又把指针置为NULL,防止野指针void sortFree2(char ***myp, int len){int i = 0;char **p = NULL;//定义个辅助指针变量,把二级指针实参接过来if (myp == NULL){return;}
    p = *myp;//还原成二级指针,即要修改的实参指针if (p == NULL){return;}
    for (i = 0; i < len; i++){free(myp[i]);}free(myp);*myp = NULL;}
    //把二级指针第1种内存模型(p1指向的地址)//和第2种内存模型(buf2指向的地址)的数据//copy到第3种内存模型(p3指向的地址)int main(){ char *p1[] = { "aaaaaaaaa", "ccccc", "bbb" };char buf2[10][30] = { "111", "333", "22222" };char **p3 = NULL;int len1, len2, len3;int i = 0;len1 = sizeof(p1) / sizeof(*p1);//p1是指针的数组,*p1就是数组首元素p1[0],即第一个char*指针(4字节)len2 = 3;
    int ret = 0;ret = copyandsort(p1, len1, buf2, len2, &p3, &len3);if (ret != 0){printf("fun sort() error: %d ", ret);return ret;}
    for (i = 0; i<len3; i++ ){printf("%s ", p3[i]);}
    system("pause");return ret;}

  • 相关阅读:
    ASCII,Unicode,UTF
    C#值类型和引用类型2
    C#中使用Foreach
    CSS基础(2)
    CSS基础
    HTML基础
    MySQL高级
    MySQL和Python交互案例练习(2)
    MySQL和Python交互案例练习(1)
    外键SQL语句的编写
  • 原文地址:https://www.cnblogs.com/fengxing999/p/12382627.html
Copyright © 2011-2022 走看看