码迷,mamicode.com
首页 > 编程语言 > 详细

C++二级指针第三种内存模型

时间:2015-07-12 12:34:16      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void main()
{
    int i = 0, j = 0;
    char buf[100];
    char **myarray = (char **)malloc(10*sizeof(char*)); //int array[10]
    if (myarray == NULL)
    {
        return;
    }
    for (i=0; i<10; i++)
    {
        myarray[i] = (char *)malloc(100*sizeof(char)); //char buf[100];
        if (myarray[i]  == NULL)
        {
            printf("ddddde\n");
            return;
        }
        sprintf(myarray[i],"%d%d%d ", i, i, i);
    }

    //第三种内存模型打印 
    printf("排序之前\n");
    for (i=0; i<10; i++)
    {
        printf("%s\n", myarray[i]);
    }

    //排序
    for (i=0; i<10; i++)
    {
        for (j=i+1; j<10; j++)
        {
            if (strcmp(myarray[i], myarray[j]) < 0)
            {
                strcpy(buf, myarray[i]);
                strcpy(myarray[i], myarray[j]);
                strcpy(myarray[j], buf);
            }
        }
    }

    //第三种内存模型打印
    printf("排序之后\n");
    for (i=0; i<10; i++)
    {
        printf("%s\n", myarray[i]);
    }

    for (i=0; i<10; i++)
    {
        free(myarray[i] );
    }
    if (myarray != NULL) 
    {
        free(myarray);
    }

    system("pause");
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int    printfArr33(char **ArrayStr, int iNum)
{
    int i = 0;
    for (i=0; i<iNum; i++)
    {
        printf("%s \n", ArrayStr[i]);
    }
    return 0;
}

int sortArrayStr03(char **ArrayStr, int iNum)
{
    int i = 0, j = 0;
    char *tmp = NULL;
    //排序

    for (i=0; i<iNum; i++)
    {
        for (j=i+1; j<iNum; j++)
        {
            if (strcmp(ArrayStr[i],ArrayStr[j]) < 0)
            {
                tmp = ArrayStr[i];
                ArrayStr[i] = ArrayStr[j];
                ArrayStr[j] = tmp;
            }
        }
    }
    return 0;
}

void main()
{
    int i = 0, j = 0;
    char buf[100];
    char **myarray = (char **)malloc(10*sizeof(char*)); //int array[10]
    if (myarray == NULL)
    {
        return;
    }
    for (i=0; i<10; i++)
    {
        myarray[i] = (char *)malloc(100*sizeof(char)); //char buf[100];
        if (myarray[i]  == NULL)
        {
            printf("ddddde\n");
            return;
        }
        sprintf(myarray[i],"%d%d%d ", i, i, i);
    }

    //第三种内存模型打印 
    printf("排序之前\n");
    printfArr33(myarray, 10);

    sortArrayStr03(myarray, 10);

    //第三种内存模型打印
    printf("排序之后\n");
    printfArr33(myarray, 10);

    for (i=0; i<10; i++)
    {
        free(myarray[i] );
    }
    if (myarray != NULL) 
    {
        free(myarray);
    }

    system("pause");
}

 

C++二级指针第三种内存模型

标签:

原文地址:http://www.cnblogs.com/CentForever/p/4640729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!