1

I tried to make a 2-D array given the numbers of rows and columns as the user input.

int main(void)
{
   int nx, ny;
   scanf_s("%d", &nx);
   scanf_s("%d", &ny);
   int array[nx][ny];
   return 0;
}

But VSC is telling me that I must have constants in the parenthesis [].

Is there any way I can convert 'nx' and 'ny' as constant?

Or is there any other way to declare 2-D or N-D arrays without converting their dtype?

patha
  • 65
  • 5

3 Answers3

1

You should use malloc or for educational purposes declare MAX size matrix and work only within nx-ny region

#define MAX 1000
int main(void)
{
   int nx, ny;
   int array[MAX][MAX];
   scanf_s("%d", &nx);
   scanf_s("%d", &ny);
   if(nx > MAX || ny > MAX) return 1; // valid range check 
   // work with array
   return 0;
}

Axifive
  • 1,159
  • 2
  • 19
  • 31
1

This is would be a way of doing it:

int main(void)
{
   int nx=0, ny=0;#intialize some random values to the variable
   scanf_s("%d", &nx);
   scanf_s("%d", &ny);
   int array[nx][ny];
   return 0;
}
BierDav
  • 1,219
  • 1
  • 10
  • 27
Garampapa
  • 11
  • 2
0

C99 introduced "Variable Length Arrays" ("VLAs"), but I would strongly discourage their use. Your best bet is to use good old malloc()

EXAMPLE:

https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int r = 3, c = 4; //Taking number of Rows and Columns
    int *ptr, count = 0, i;
    ptr = (int *)malloc((r * c) * sizeof(int)); //Dynamically Allocating Memory
    for (i = 0; i < r * c; i++)
    {
        ptr[i] = i + 1; //Giving value to the pointer and simultaneously printing it.
        printf("%d ", ptr[i]);
        if ((i + 1) % c == 0)
        {
            printf("\n");
        }
    }
    free(ptr);
}

ADDENDUM:

MSVS might support VLAs in version 16.8 or higher: https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    why do you write `(int *)` before `malloc`. Surely this can just cause suppression of compiler warnings, for no benefit – M.M Jul 21 '21 at 01:50
  • 1
    Also this dynamically allocates a 1-D array and is mis-titled – M.M Jul 21 '21 at 01:50
  • 2
    *C99 introduced "Variable Length Arrays" ("VLAs"), but I would strongly discourage their use.* That's misguided - at best. Despite Microsoft's protestations and insistence on remaining stuck in the last century, VLAs are extremely useful and no more "dangerous" to memory than any other C construct. See [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) for just one example of how powerful and perfectly safe VLAs can be. – Andrew Henle Jul 21 '21 at 16:12
  • In the page you linked they say: *"Astute readers will note that VLAs are also not supported. Variable length arrays are generally less efficient than comparable fixed sized arrays, and generally inefficient compared to equivalent malloc(), when safely and securely implemented. VLAs provide attack vectors comparable to those of the infamous gets() — deprecated and destined to removal — for opportunities of “shifting the stack” and other exploits. For these reasons **we intend not to support VLAs as an optional feature in C11**."*. That doesn't really sounds like MSVC "might" support VLAs... – Bob__ Jul 22 '21 at 15:24