怎样正确设置,访问,并释放在C多维数组? [英] How do I correctly set up, access, and free a multidimensional array in C?

查看:108
本文介绍了怎样正确设置,访问,并释放在C多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几十个有关的问题有什么错我的code关于C.多维数组出于某种原因,人们似乎无法换他们的身边这里发生了什么头,所以我决定来回答这个问题至于其他的引用:

I have seen dozens of questions about "what’s wrong with my code" regarding multidimensional arrays in C. For some reason people can’t seem to wrap their head around what is happening here, so I decided to answer this question as a reference to others:

我如何正确设置,接入和免费多维C中的数组?

How do I correctly set up, access, and free a multidimensional array in C?

如果别人有帮助的建议,请随时一起发布!

If others have helpful advice, please feel free to post along!

推荐答案

在C,因为C C99,甚至是动态的多维数组,可以轻松地一次过拨款与的malloc 和释放与免费

In C since C99, even dynamic multidimensional arrays can be easily allocated in one go with malloc and freed with free:

double (*A)[n] = malloc(sizeof(double[n][n]));

for (size_t i = 0; i < n; ++i)
  for (size_t j = 0; j < n; ++j)
      A[i][j] = someinvolvedfunction(i, j);

free(A);

这篇关于怎样正确设置,访问,并释放在C多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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