多维指针

void main2()
{
    int i = 0;

    //指针数组
    char *   p1[] = {"123", "456", "789"};


    //二维数组
    char p2[3][4]  = {"123", "456", "789"};

    //二维内存
    char **p3 = (char **)malloc(3 * sizeof(char *)); //int array[3];

    for (i=0; i<3; i++)
    {
        p3[i] = (char *)malloc(10*sizeof(char)); //char buf[10]

        sprintf(p3[i], "%d%d%d", i, i, i);
    }
}

数组指针类型
数组指针用于指向一个数组
int a[10]
数组名是数组首元素的起始地址,但并不是数组的起始地址
通过将取地址符&作用于数组名可以得到整个数组的起始地址
//定义数组指针 有两种
1)通过数组类型定义数组指针:
typedef int(ArrayType)[5]; int *a
ArrayType* pointer;
2) 声明一个数组指针类型 typedef int (*MyPointer)[5];
MyPointer myPoint;
3)直接定义:int (*pointer)[n];
pointer 为数组指针变量名
type 为指向的数组的类型
n 为指向的数组的大小
注意这个地方是type类型(比如 int (*pointer)[10])

数组指针:用数组类型加*定义一个数组指针

{
    int a[5];
    //声明一个数组类型
    typedef int(MYINT5)[5];
    //用数组类型 加*,定义一个数组指针变量
    MYINT5 *array;
    array = &a;
    for (i=0; i<5; i++)
    {
        (*array)[i] = i;
    }
    //
    for (i=0; i<5; i++)
    {
        printf("\n%d %d", a[i], (*array)[i]);
    }
}

数组指针:定义一个数组指针类型,然后用类型定义变量

{
    int b[5];
    //声明一个数组指针类型
    typedef int (*MyPointer)[5];
    //用数组指针类型,去定义一个变量
    MyPointer mypoint ;
    mypoint= &b;    
    for (i=0; i<5; i++)
    {
        (*mypoint)[i] = i;
    }
    //
    for (i=0; i<5; i++)
    {
        printf("\n%d %d", b[i], (*mypoint)[i]);
    }
}

//3数组指针:直接定义一个数组指针变量
{
int c[5];
//直接声明一个数组指针变量
int (*pointer)[5] = &c;
for (i=0; i<5; i++)
{
(*pointer)[i] = i;
}
for (i=0; i<5; i++)
{
printf(“\n%d %d”, c[i], (*pointer)[i]);
}
}
多维数组

void main()
{
    int a[3][5];

    int c[5]; //&c + 1;
    int b[10]; //b代表数组首元素的地址 &b代表这个数组的地址 &b+1相当于 指针后移4*10个单位

    printf("a:%d, a+1:%d \n", a, a+1); //4*5

    {
        int i=0, j = 0, tmp = 0;
        for (i=0; i<3; i++)
        {
            for (j=0; j<5; j++)
            {
                a[i][j] = ++tmp;
            }
        }

        printf("\n");
        for (i=0; i<3; i++)
        {
            for (j=0; j<5; j++)
            {
                printf("%d \n", a[i][j]);
            }
        }
    }

    //a的本质是一个数组指针,每次往后跳一维的维数
    {
        int i = 0, j = 0;
        //定义了一个数组指针 变量
        int (*myArrayPoint)[5] ; //开辟四个字节内存
        myArrayPoint  =  a;
        printf("\n");
        for (i=0; i<3; i++)
        {
            for (j=0; j<5; j++)
            {
                //myArrayPoint[i][j] = ++tmp;
                printf("%d \n", myArrayPoint[i][j]);
            }
        }
    }

    /*
    char cbuf[30]; // cbuf(1级指针) 代表数组首元素的地址。。。&cbuf(二级指针) 代表整个数组的地址
    char array[10][30]; //array是二级指针
    (array+i) //相当于 整个第i行的数组地址 //二级指针 &cbuf

    (*(array+i))//一维数组的首地址 cbuf

    (*(array+i))+j //相当于第i行第j列的地址。。。。&array[i][j]

    *((*(array+i))+j) //相当于第i行第j列的地址。。。。<====>array[i][j]
    */

    system("pause");
}

多维数组做函数参数退化

void printfArray411(int *array, int num)
{
    int i = 0;
    for (i=0; i<num ; i++)
    {
        printf("%d ", array[i]);
    }
}

void printfArray412(int (*array)[5], int num)
{
    return ;
}

void printfArrr333(int c[3][4][5])
{
    return ;
}
void main()
{
    int a[3][5];
    int c[3][4][5];
    int i , j = 0;
    int tmp = 0;
    for (i=0; i<3; i++)
    {
        for (j=0; j<5; j++)
        {
            a[i][j] = tmp ++;
        }
    }


    printfArray411((int *)a, 15);

    system("pause");
}

指针数组

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int getArray3_Free(char **p3, int p3num)
{
    int i;
    if (p3 == NULL)
    {
        return -1;
    }
    for (i=0; i<p3num; i++)
    {
        if (p3[i]!=NULL)
        {
            free(p3[i]);
        }
    }
    free(p3);
}


int getArray3_Free2(char ***p3, int p3num)
{
    int i;
    char **tmp = NULL;

    if (p3 == NULL)
    {
        return -1;
    }
    tmp = *p3;

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

    *p3 = NULL; //通过间接赋值,去间接的修改实参的值,成0
}

int getArray3_2(char **myp1, int num1, char  (*myp2)[30], int num2, char ***       myp3, int *num3)
{
    int ret = 0;
    int i,j;
    int tmpNum3 = 0;

    char **tmpp3 = NULL;
    char *temp;

    /*
    printf("111111111");
    if (*myp3 ==NULL )
    {
        printf("222222222");
    } */
    printf("33333");
    if (myp1==NULL || myp2==NULL ||num3==NULL || myp3==NULL)
    {
        ret = -1;
        return ret;
    }
    //准备内存
    tmpNum3 = num1 + num2;
    //分配第一维
    tmpp3 = (char **)malloc(tmpNum3 * sizeof(char *));
    if (tmpp3 == NULL)
    {
        return NULL;
    }

    //分配第二维 把第一种内存模型数据和第二种内存模型数据,copy到第3中内存模型中
    for (i=0; i<num1; i++)
    {
        tmpp3[i] = (char *)malloc(strlen(myp1[i])+1);
        if (tmpp3[i]==NULL)
        {
            puts("out of space");
            return NULL;
        }
        strcpy(tmpp3[i],myp1[i]);
    }
    for (j=0;j<num2;j++,i++)
    {
        tmpp3[i]=(char *)malloc(strlen(myp2[j]) + 1); //note modify
        if (tmpp3[i]==NULL)
        {
            puts("out of space");
            return NULL;
        }
        strcpy(tmpp3[i],myp2[j]);
    }

    //排序
    for (i=0;i<tmpNum3;i++)
    {
        for (j=i+1;j<tmpNum3;j++)
        {
            if (strcmp(tmpp3[i],tmpp3[j])>0)
            {
                temp=tmpp3[i];
                tmpp3[i]=tmpp3[j];
                tmpp3[j]=temp;
            }
        }
    }

    //通过间接赋值,把结果甩给实参
    *num3=tmpNum3;
    *myp3 = tmpp3; //*0 = 100;
    return ret;
}

char **getArray3(char **myp1, int num1, char  (*myp2)[30], int num2, int *num3)
{
    int i,j;
    int tmpNum3 = 0;

    char **tmpp3 = NULL;
    char *temp;

    if (myp1==NULL || myp2==NULL ||num3==NULL )
    {
        return NULL;
    }
    //准备内存
    tmpNum3 = num1 + num2;
    //分配第一维
    tmpp3 = (char **)malloc(tmpNum3 * sizeof(char *));
    if (tmpp3 == NULL)
    {
        return NULL;
    }

    //分配第二维 把第一种内存模型数据和第二种内存模型数据,copy到第3中内存模型中
     for (i=0; i<num1; i++)
     {
        tmpp3[i] = (char *)malloc(strlen(myp1[i])+1);
        if (tmpp3[i]==NULL)
        {
            puts("out of space");
            return NULL;
        }
        strcpy(tmpp3[i],myp1[i]);
     }
     for (j=0;j<num2;j++,i++)
     {
         tmpp3[i]=(char *)malloc(strlen(myp2[j]) + 1); //note
         if (tmpp3[i]==NULL)
         {
             puts("out of space");
             return NULL;
         }
         strcpy(tmpp3[i],myp2[j]);
     }

     //排序
     for (i=0;i<tmpNum3;i++)
     {
         for (j=i+1;j<tmpNum3;j++)
         {
             if (strcmp(tmpp3[i],tmpp3[j])>0)
             {
                 temp=tmpp3[i];
                 tmpp3[i]=tmpp3[j];
                 tmpp3[j]=temp;
             }
         }
     }

     *num3=tmpNum3;
    return tmpp3;
}

void main()
{
    int num3 = 0, i = 0; 
    int ret = 0;
    char *p1[] = {"222222", "1111111", "33333333"};
    char p2[4][30] =  {"bbbbb", "aaaaa", "zzzzzz", "ccccccc"};
    char **p3 = NULL;
    char ***myerrp3 = NULL;

    //p3 = getArray3(p1, 3, p2, 4, &num3);
    //ret = getArray3_2(p1,3, p2, 4, &p3, &num3);
    ret = getArray3_2(p1,3, p2, 4, 0, &num3); //错误做法
    if (ret != 0)
    {
        return ;
    }
    for (i=0; i<num3; i++)
    {
        printf("%s \n", p3[i]);
    }

    //getArray3_Free(p3, num3);
    //  p3=NULL;
    getArray3_Free2(&p3, num3);

    printf("p3:%d \n", p3);

    system("pause");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值