How to declare a variable size 2D array in C?

前端 未结 3 1301
太阳男子
太阳男子 2021-01-02 10:07

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 image

3条回答
  •  猫巷女王i
    2021-01-02 10:29

    If you have a modern C compiler (at least C99) in function scope it is as simple as:

    unsigned arr[n][m];
    

    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]));
    

    and later

    free(arr);
    

提交回复
热议问题