在文件范围错误动态可变“变量名”? [英] Variably modified 'variable_name' at file scope error?

查看:155
本文介绍了在文件范围错误动态可变“变量名”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新建C.编译时,我发现了以下错误:

New to C. I'm getting the following error when compiling:

error: variably modified 'header' at file scope
error: variably modified 'sequence' at file scope

code:

struct list{
  char header[list_header_size];
  char sequence[list_sequence_size];
  struct list *next;
};

我认为错误意味着需要编译器知道这些变量是从一开始。所以,我提出的main(),这是在该结构被调用时,该节目的结束。我也试过在程序的开头声明变量,但我不知道如果我这样做正确。我试图焦头; 字符头[];

推荐答案

您是对的,编译器需要知道的类型结构的成员的。为什么它需要知道的类型的一个原因是,这样它可以计算出大小。在你的情况,然而,它不能知道,因为在你的结构已定义了两个阵列是恒定大小的未大小。因此,编译器不知道该结构的总规模,这违背知道类型的目的。

You are right that the compiler needs to know the types of the members of the struct. One reason why it needs to know the types is so that it can calculate sizes. In your case, however, it can't know the sizes because in your struct you have defined two arrays that are not of a constant size. Therefore, the compiler doesn't know the total size of the struct and this defeats the purpose of knowing the types.

最接近你想要的是有两个字符指针来代替两个字符数组和分配内存他们将指向动态。

The closest to what you want is to replace the two char arrays with two char pointers and allocate the memory they will point to dynamically.

这篇关于在文件范围错误动态可变“变量名”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆