谁帮个忙,改下程序

craboy1 2013-10-25 09:56:37
现在只用到二级指针,我现在想用到三级指针,即创建一个二维数组,元素是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;
}
...全文
186 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
craboy1 2013-10-25
  • 打赏
  • 举报
回复
引用 3 楼 max_min_ 的回复:

arr = (struct el**)calloc(10,sizeof(struct el ));
        for(i=0;i<10;i++){
                arr[i] = malloc(sizeof(struct el) * 10);
 //因为是两重循环所以这样赋值错的,应该两重循环赋值打印!试试!具体可参考int类型,         
        
两重循环赋值,再加一个循环?没想到怎么用!!
liweiiewil 2013-10-25
  • 打赏
  • 举报
回复
#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; }
craboy1 2013-10-25
  • 打赏
  • 举报
回复
在学libevent,其中上边的测试struct el其实是struct event_list,libevent处理优先级就用到了struct event_list **activequeues; 曾经也看过lighttp,就用到了三级指针。
max_min_ 2013-10-25
  • 打赏
  • 举报
回复

arr = (struct el**)calloc(10,sizeof(struct el ));
        for(i=0;i<10;i++){
                arr[i] = malloc(sizeof(struct el) * 10);
 //因为是两重循环所以这样赋值错的,应该两重循环赋值打印!试试!具体可参考int类型,         
        
Isnis-fallen 2013-10-25
  • 打赏
  • 举报
回复
下面这个程序就是实现用一个三级指针来指向一个二维数组的方法.如果是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);
}
做或不做 2013-10-25
  • 打赏
  • 举报
回复
三级指针 是大神才用得起的 至少我从来未敢涉足 我在想 什么事情是必须得用三级指针的 你要做什么 实验还是 说有特殊需求
zhao 2013-10-25
  • 打赏
  • 举报
回复
在你的基础上修改了一下,不知合你意不?申请二维数组的话不必用三级指针!

#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;
}
赵4老师 2013-10-25
  • 打赏
  • 举报
回复
仅供参考
//在堆中开辟一个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
//--------------------
//=======================
//

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧