用C二维动态数组的大小变化 [英] Change size of 2D dynamic array in C

查看:172
本文介绍了用C二维动态数组的大小变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个二维动态数组:

  A =(INT **)释放calloc(N-1的sizeof(INT));
对于(I = 0; I&≤(N-1);我+ +)
    一个由[i] =(INT *)释放calloc(N,的sizeof(INT));

然后我需要改变它的大小(添加新行):

  A =(INT **)的realloc(A,N);
一个[N] =(INT *)释放calloc(N,的sizeof(INT));

但是,当我想打印我的阵列,

 无效打印(无效){
    INT I,J;
    对于(I = 0; I&≤(N-1);我++){
        为(J = 0; J< N; J ++){
            的printf(%d个\\ t的,编曲[I] [J]);
        }
        的printf(\\ n);
    }
}

我有访问冲突。第一行是印...
我应该怎么办?


解决方案

  A =(INT **)的realloc(A,(N + 1)* sizeof的为(int *));
Ñ​​++;

I create a 2D dynamic array:

a = (int**)calloc(n-1, sizeof(int));
for(i = 0; i < (n-1); i++)
    a[i] = (int*)calloc(n, sizeof(int));

Then I need to change its size (add new line):

a = (int**)realloc(a, n);
a[n] = (int*)calloc(n, sizeof(int));

But when i want to print my array,

void Print(void){
    int i, j;
    for(i = 0; i < (n-1); i++){
        for(j = 0; j < n; j++){
            printf("%d\t", arr[i][j]);
        }
        printf("\n");
    }
}

i have access violation. First line is printed... What should I do?

解决方案

a = (int**)realloc(a, (n + 1) * sizeof(int *));
n++;

这篇关于用C二维动态数组的大小变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆