如何申报在C可变大小的二维数组? [英] How to declare a variable size 2D array in C?
问题描述
我有一个项目出现问题。我必须做出一个可变大小的二维数组用于存储一些prediction error..so这是关于图像。麻烦的是,我需要加载不同大小的图像,以便为每个图像,我将不得不进入搜索您的问题中的二维数组pixels..I've的coresponding号文件,但它不是我要找for.Can谁能帮助我?
I have a problem with a project. I have to make a variable size 2D array for storing some prediction error..so this is about images. The trouble is that I have to load images of different sizes so for each image I would have to get into a file the 2D array with the coresponding number of pixels..I've searched among your questions but it's not what I'm looking for.Can anyone help me?
感谢您
推荐答案
如果你有一个现代的C编译器(至少C99)的功能范围很简单:
If you have a modern C compiler (at least C99) in function scope it is as simple as:
unsigned arr[n][m];
这就是所谓的可变长度阵列(VLA)。它可能有问题,如果数组过大。所以,如果您有大量的图像,你可以这样做:
this is called a variable length array (VLA). It may have problems if the array is too large. So if you have large images you could do:
unsigned (*arr)[m] = malloc(sizeof(unsigned[n][m]));
和后来
free(arr);
这篇关于如何申报在C可变大小的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!