Home > Software engineering >  how do I let users input a dynamic 2D array?
how do I let users input a dynamic 2D array?

Time:11-26

int main(){
int limit_x;
int limit_y;
scanf("%d", &limit_x);
scanf("%d", &limit_y);
char map[limit_x][limit_y];
    for (int index_x=0;index_x<limit_x;index_x  )
    {
        for (int index_y = 0; index_y < limit_y; index_y  )
        {
            scanf("%c", &map[index_x][index_y]);
        }
    }
}

This is how I try to do it , but it is not working for me . it shows error C2057,C2466.

Let users input the row and col, and make a dynamic 2d array map by input.

CodePudding user response:

int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
char** map = malloc(sizeof(char*) * x);
for (int index=0; index < x; index  )
{
    map[index] = malloc(sizeof(char) * y);
}
for (int index_x=0;index_x<x;index_x  )
{
    for (int index_y = 0; index_y < y; index_y  )
    {
        scanf("%c", &map[index_x][index_y]);
    }
}

now is working for me thx alot @UnholySheep;

CodePudding user response:

it shows error C2057,C2466.

It appears to be warnings "C2057 expected constant expression" and "C2466 cannot allocate an array of constant size 0". That's nonsense. The C compiler should not expect a constant expression and there is no array of size zero.

You get these warnings because your compiler is non-conforming to the C language. Please ensure that you compiled as C and not C . If you still have problems, you should switch to a compiler from year 1999 or later. I recommend using gcc.

(Note: compilers conforming to C11 or C17 may declare __STDC_NO_VLA__ and then they may claim to be compliant, given that they declared __STDC__ in the first place. This is not the case for Microsoft Visual C.)

CodePudding user response:

The Microsoft compiler does not support variable-length arrays. The ISO C standard also does not require that compilers support them, because since ISO C11, it is an optional feature.

If you want to have equivalent functionality as variable-length arrays with a Microsoft Compiler, you have these two options:

  • Use malloc instead. However, this will also require you to use free when you no longer need the memory, otherwise you will have a memory leak.

  • Use the platform-specific function _alloca, which will cause the memory to automatically be freed when the calling function returns.

However, both of these solutions do not allow you to create an actual 2D array, only a 1D array. Therefore, you will have to calculate the offsets manually to "simulate" a 2D array. Here is an example which uses _alloca_:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main ( void )
{
    int limit_x;
    int limit_y;
    char *map;

    scanf("%d", &limit_x);
    scanf("%d", &limit_y);

    //discard newline character
    getchar();

    //allocate map as a 1D array
    map = _alloca( limit_x * limit_y );

    for ( int index_x=0; index_x < limit_x; index_x   )
    {
        for ( int index_y = 0; index_y < limit_y; index_y   )
        {
            //simulate a 2D array by calculating the offsets into
            //the 1D array manually
            scanf("%c", &map[index_x*limit_y index_y]);
        }
    }

    //print the array for testing purposes
    for ( int index_x=0; index_x < limit_x; index_x   )
    {
        printf(
            "Characters in outer array element #%d: %.*s\n",
            index_x, limit_y, map index_x*limit_y
        );
    }
}

For the input

4
5
0123456789ABCDEFGHIJ

this program has the following output:

Characters in outer array element #0: 01234
Characters in outer array element #1: 56789
Characters in outer array element #2: ABCDE
Characters in outer array element #3: FGHIJ
  •  Tags:  
  • c
  • Related