首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

哪位高手帮个忙,改下程序

2013-11-29 
谁帮个忙,改下程序现在只用到二级指针,我现在想用到三级指针,即创建一个二维数组,元素是struct el,但不要

谁帮个忙,改下程序
现在只用到二级指针,我现在想用到三级指针,即创建一个二维数组,元素是struct el,但不要用数组定义,全用申请内存的方式。C基础不好,没思路了

#include<stdio.h>
#include<malloc.h>
struct el{
        int a;
};
int main(int argc, char **argv){
        struct el **arr;
        int i;
        arr = (struct el**)calloc(10,sizeof(struct el *));
        for(i=0;i<10;i++){
                arr[i] = malloc(sizeof(struct el));
                arr[i]->a=i;
        }
        for(i=0;i<10;i++){
                printf("arr[%d]:%d\n",i,arr[i]->a);
        }
        return 0;
}

[解决办法]
三级指针 是大神才用得起的 至少我从来未敢涉足
我在想 什么事情是必须得用三级指针的 你要做什么 实验还是 说有特殊需求
[解决办法]
下面这个程序就是实现用一个三级指针来指向一个二维数组的方法.如果是4维,5维,那么指针级数也随着增加.

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

struct test
{
                int a;
};


int main( int argc, char **argv )
{
        if( argc != 3 )
        {
                printf( "Usage: proram first_arry_size second_arry_size\n" );
                return -1;
        }
        struct test ***tmp;
        int i, j, first_arry_size, second_arry_size;
        first_arry_size = atoi(argv[1]);
        second_arry_size = atoi(argv[2]);
        tmp = (struct test ***)malloc(first_arry_size*sizeof(struct test **));
        for( i=0; i<first_arry_size; i++ )
        {
                tmp[i] = (struct test **)malloc(second_arry_size*sizeof(struct test *));
                for( j=0; j<second_arry_size; j++ )
                {
                        tmp[i][j] = (struct test *)malloc(sizeof(struct test));
                        tmp[i][j]->a = i*10+j;
                }
        }
        for( i=0; i<first_arry_size; i++ )
  {
    for( j=0; j<second_arry_size; j++ )
                {
                        printf( "arry[%d][%d]: %d\n", i, j, tmp[i][j]->a );
                        free(tmp[i][j]);
                }
                free(tmp[i]);
  }
  free(tmp);
}

[解决办法]

arr = (struct el**)calloc(10,sizeof(struct el ));
        for(i=0;i<10;i++){
                arr[i] = malloc(sizeof(struct el) * 10);
 //因为是两重循环所以这样赋值错的,应该两重循环赋值打印!试试!具体可参考int类型,         


        


[解决办法]
#include<stdio.h>
#include<malloc.h>
struct el{
        int a;
};
int main(int argc, char **argv)
{
        struct el ***arr;
        int i,j;
        arr = (struct el***)calloc(10,sizeof(struct el **));
        for(i=0; i<10; ++i)
        {
            arr[i] = (struct el**)calloc(10,sizeof(struct el *));
            for(j=0;j<10;++j)
            {
                arr[i][j] = (struct el*)malloc(sizeof(el));
                arr[i][j]->a=i;
            }
        }
        for(i=0;i<10;i++){
                for(j=0;j<10;++j)
                {
                    printf("arr[%d][%d]:%d\n",i,j,arr[i][j]->a);
                }

        }
        return 0;
}

[解决办法]
仅供参考
//在堆中开辟一个3×4×5的3维int数组
#include <stdio.h>
#include <malloc.h>
int ***p;
int i,j,k;
void main() {
    p=(int ***)malloc(3*sizeof(int **));
    if (NULL==p) return;
    for (i=0;i<3;i++) {
        p[i]=(int **)malloc(4*sizeof(int *));
        if (NULL==p[i]) return;
        for (j=0;j<4;j++) {
            p[i][j]=(int *)malloc(5*sizeof(int));
            if (NULL==p[i][j]) return;
        }
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            for (k=0;k<5;k++) {
                p[i][j][k]=i*20+j*5+k;
            }
        }
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            for (k=0;k<5;k++) {
                printf(" %2d",p[i][j][k]);
            }
            printf("\n");
        }
        printf("---------------\n");
    }
    for (i=0;i<3;i++) {
        for (j=0;j<4;j++) {
            free(p[i][j]);
        }
        free(p[i]);
    }
    free(p);
}
//  0  1  2  3  4
//  5  6  7  8  9
// 10 11 12 13 14
// 15 16 17 18 19
//---------------
// 20 21 22 23 24
// 25 26 27 28 29
// 30 31 32 33 34
// 35 36 37 38 39
//---------------
// 40 41 42 43 44
// 45 46 47 48 49
// 50 51 52 53 54
// 55 56 57 58 59
//---------------

//在堆中开辟一个2×3×4×5的4维int数组
#include <stdio.h>
#include <malloc.h>
int ****p;
int h,i,j,k;
void main() {
    p=(int ****)malloc(2*sizeof(int ***));
    if (NULL==p) return;
    for (h=0;h<2;h++) {


        p[h]=(int ***)malloc(3*sizeof(int **));
        if (NULL==p[h]) return;
        for (i=0;i<3;i++) {
            p[h][i]=(int **)malloc(4*sizeof(int *));
            if (NULL==p[h][i]) return;
            for (j=0;j<4;j++) {
                p[h][i][j]=(int *)malloc(5*sizeof(int));
                if (NULL==p[h][i][j]) return;
            }
        }
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                for (k=0;k<5;k++) {
                    p[h][i][j][k]=h*60+i*20+j*5+k;
                }
            }
        }
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                for (k=0;k<5;k++) {
                    printf(" %3d",p[h][i][j][k]);
                }
                printf("\n");
            }
            printf("--------------------\n");
        }
        printf("=======================\n");
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                free(p[h][i][j]);
            }
            free(p[h][i]);
        }
        free(p[h]);
    }
    free(p);
}
//   0   1   2   3   4
//   5   6   7   8   9
//  10  11  12  13  14
//  15  16  17  18  19
//--------------------
//  20  21  22  23  24
//  25  26  27  28  29
//  30  31  32  33  34
//  35  36  37  38  39
//--------------------
//  40  41  42  43  44
//  45  46  47  48  49
//  50  51  52  53  54
//  55  56  57  58  59
//--------------------
//=======================
//  60  61  62  63  64
//  65  66  67  68  69
//  70  71  72  73  74
//  75  76  77  78  79
//--------------------
//  80  81  82  83  84
//  85  86  87  88  89
//  90  91  92  93  94
//  95  96  97  98  99
//--------------------
// 100 101 102 103 104
// 105 106 107 108 109
// 110 111 112 113 114
// 115 116 117 118 119
//--------------------
//=======================
//


[解决办法]
在你的基础上修改了一下,不知合你意不?申请二维数组的话不必用三级指针!

#include<stdio.h>
#include<malloc.h>
struct el{
        int a;


};
int main(int argc, char **argv){
        struct el **arr;
        int i,j,num = 0;
        arr = (struct el**)calloc(10,sizeof(struct el *));
        for(i=0;i<10;i++){
                arr[i] = malloc(sizeof(struct el) * 5);
                for(j = 0;j < 5;j++)
                {
                arr[i][j].a = num++;
                }
        }
        for(i=0;i<10;i++){
             for(j = 0;j < 5;j++)
                {
                printf("%d\n",arr[i][j].a);
                }   
        }
        return 0;
}

热点排行
Bad Request.