简体   繁体   English

C中的动态数组大小

[英]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 ? 我该如何克服呢?

In the older C versions (eg C89), you couldn't declare an array with variable length. 在较早的C版本(例如C89)中,无法声明具有可变长度的数组。 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 . 尽管如此,它还是以可变长度数组的名称添加到C中(在C99中)。 Therefore, in the past you could only do: 因此,过去您只能执行以下操作:

int array[CONSTANT_EXPRESSION];

but with C99 or above you can do: 但是使用C99或更高版本,您可以执行以下操作:

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 . 一种是使用C99 Ie, tell your compiler to enable C99 features. 即,告诉编译器启用C99功能。 Your second solution, which I'd certainly recommend is using malloc . 我肯定会推荐的第二种解决方案是使用malloc

Create table as a matrix to which you will set the size with malloc : 创建table作为矩阵,您将使用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 : 要拥有一个可变大小的数组,通常可以使用malloc

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

In C89 variable length array was not present. 在C89中,不存在可变长度数组。 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. 在C99中引入了可变长度数组,因此您可以在运行时计算值并使用它们来定义数组的尺寸。

Either use C99 with -std=c99 flag with gcc and vla. 将C99与-std=c99标志一起用于gcc和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;
}

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 . 如果someParameter是真正动态的(无法在编译时求值),则必须使用malloc为表动态分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM