【问题标题】:Can we use this approach to allocate a 2d matrix- //let require dimensions be i*j int **p=(int **)malloc(sizeof(int)*i*j);我们可以使用这种方法分配一个二维矩阵吗? //让要求维度为 i*j int **p=(int **)malloc(sizeof(int)*i*j);
【发布时间】:2015-07-25 11:40:10
【问题描述】:

我们可以用这种方法来分配一个二维矩阵吗-

//let require dimensions be i*j     
int **p=(int **)malloc(sizeof(int)*i*j);

【问题讨论】:

    标签: malloc


    【解决方案1】:

    你总是可以尝试,但它不会像你期望的那样。

    分配给i*jints,这很好。但是当你访问p 时,我猜你期望做的p[x][y] 不会像你期望的那样。

    p[x] 的类型为int*,因此程序将查找内存地址,并将y * sizeof(int) 添加到该地址,这就是您的值。

    更多详情请参阅this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      相关资源
      最近更新 更多