Maciek Gaweł
Maciek Gaweł

Reputation: 27

Dynamic array size in C

I've got following code

#include <stdio.h>
#include <string.h>

int main()
{
  char a[10000] = "aaaaaaaa"; // this attribute may vary, so the length isn't going to be constant
  int aLength = strlen(a);
  int someParameter = aLength /4; // some attribute that's computed dynamically 

  char table[aLength][someParameter];
}

And on the table declaration I get error -> "Expression must have a constant value". How can I get over this ?

Upvotes: 0

Views: 190

Answers (4)

phoxis
phoxis

Reputation: 61910

In C89 variable length array was not present. You need to provide the length which is available at compile time.

In C99 variable length arrays were introduced, therefore you can calculate values at runtime and use them to define the dimensions of the array.

Either use C99 with -std=c99 flag with gcc and vla. OR use something like below.

char get_array (int d1, int d2)
{
  char **arr;
  int i;

  arr = malloc (sizeof (char *) * d1);
  for (i=0; i<d1; i++)
  {
    arr[i] = malloc (sizeof (char) * d2);
  }

  return arr;
}

and to free

void free_arr (char **arr, int d1, int d2)
{
  int i;

  if (arr == NULL)
  { return; } /* caller's responsibility to assign NULL to
               * uninitialized arrays
               */

  for (i=0; i<d1; i++)
  {
    free (arr[i]);
  }
  free (arr);

  return;
}

Upvotes: 2

Marius
Marius

Reputation: 2273

You need a constant expression inside the square brackets. This would be valid:

char a[] = "aaaa";
char table[sizeof(a)-1][(sizeof(a)-1)/4];

If someParameter is truly dynamic (cannot be evaluated at compile-time) you have to dynamically allocate the memory for your table using malloc.

Upvotes: 0

fedorqui
fedorqui

Reputation: 289495

Create table as a matrix to which you will set the size with malloc:

#include <stdio.h>
#include <string.h>

int main()
{
  char a[10000] = "aaaaaaaa"; // this attribute may vary, so the length isn't going to be constant
  int aLength = strlen(a);
  int someParameter = aLength /4; // some attribute that's computed dynamically 
  char **table;

  // Allocate aLength arrays of someParameter chars
  table  = malloc(aLength * sizeof(*table));
  for(i = 0; i < aLength; i++)
     table[i] = malloc(someParameter * sizeof(*table[i]));
}

To have a variable size array in general you can use malloc:

// For example, allocate n chars
char *str  = malloc(length * sizeof(*str));

Upvotes: 2

Shahbaz
Shahbaz

Reputation: 47493

In the older C versions (e.g. C89), you couldn't declare an array with variable length. Non-constant array size made things complicated and wasn't overall very useful since the variable could be very big and immediately overflow the stack.

Nevertheless, it was added to C (in C99) under the name variable length arrays. Therefore, in the past you could only do:

int array[CONSTANT_EXPRESSION];

but with C99 or above you can do:

some_variable = whatever_expression();
int array[some_variable];

Of course, you should take care for the size of the variable length array to be positive and not too large.

You have two solutions therefore. One is to use C99. I.e., tell your compiler to enable C99 features. Your second solution, which I'd certainly recommend is using malloc.

Upvotes: 7

Related Questions