3

Possible Duplicate:
C compile error: “Variable-sized object may not be initialized”

I've got a problem cause my compiler still gives me an error: Variable-sized object may not be initialized. What's wrong with my code?

int x, y, n, i;
printf ("give me the width of the table \n");
scanf ("%d", &x);
printf ("give me the height of the table\n");
scanf ("%d", &y);
int plansza [x][y] = 0;
int plansza2 [x][y] = 0;

Of course I want to fill the table with 'zeroes'.

Unfortunately the program still doesn't work. The tables are displayed with numbers like '416082' on all of their cells. My code looks like this now.:

int plansza [x][y];
memset(plansza, 0, sizeof plansza);
int plansza2 [x][y];
memset(plansza2, 0, sizeof plansza2);

printf("plansza: \n");
for(j=0;j<x;j++) {
  for(l=0;l<y;l++) {
    printf("%d",plansza[x][y]);
    printf(" ");
  }
  printf("\n");
}

printf("plansza2: \n");
for(m=0;m<x;m++) {
  for(o=0;o<y;o++) {
    printf("%d",plansza2[x][y]);
    printf(" ");
  }
  printf("\n");
}
Community
  • 1
  • 1
fragon
  • 3,391
  • 10
  • 39
  • 76

3 Answers3

10

Your two arrays are variable lenght arrays. You cannot initialize a variable length array in C.

To set all the int elements of your arrays to 0 you can use the memset function:

memset(plansza, 0, sizeof plansza);

By the way to initialize an array which is not a variable length array, the valid form to initialize all the elements to 0 is:

int array[31][14] = {{0}};  // you need the {}
ouah
  • 142,963
  • 15
  • 272
  • 331
  • OK. I get it. So what should I do to make the table size customized by the user? Is it possible? – fragon Jan 06 '13 at 21:27
  • @user1828352 you declare them but you don't initialize them: remove the `= 0` part. – ouah Jan 06 '13 at 21:29
  • is it ok right now?:`int plansza [x][y]; memset(plansza, 0, sizeof plansza); int plansza2 [x][y]; memset(plansza2, 0, sizeof plansza2);` – fragon Jan 06 '13 at 21:32
1

If both dimensions are unknown, you'll have to use a one-dimensional array, and do the indexing yourself:

int *ary = (int *) malloc(x * y * sizeof(int));
memset(ary, 0, x * y * sizeof(int));
int elem1_2 = ary[1 * x + 2];
int elem3_4 = ary[3 * x + 4];

and so on. You better define some macros or access functions. And free the memory after use.

Chris
  • 4,133
  • 30
  • 38
1

Alternative to the suggestion of @Chris:

You can create 2d array as an array of one-dimensional arrays. This will allow you to do array element indexing as you used to. Be aware that in this case array is allocated in heap, not in stack. Therefore, when you don't need the array anymore, you must clean the allocated memory.

Example:

#include <malloc.h>
#include <string.h>

/* Create dynamic 2-d array of size x*y.  */
int** create_array (int x, int y)
{
  int i;
  int** arr = (int**) malloc(sizeof (int*) * x);
  for (i = 0; i < x; i++)
    {
      arr[i] = (int*) malloc (sizeof (int) * y);
      memset (arr[i], 0, sizeof (arr[i]));
    }
  return arr;
}

/* Deallocate memory.  */
void remove_array (int** arr, int x)
{
  int i;
  for (i = 0; i < x; i++)
    free (arr[i]);
  free (arr);
}

int main()
{
  int x = 5, y = 10;
  int i, j;
  int** plansza = create_array (x, y); /* Array creation.  */
  plansza[1][1] = 42; /* Array usage.  */
  remove_array (plansza, x); /* Array deallocation.  */
  return 0;
}
Pavel Zaichenkov
  • 835
  • 5
  • 12