简体   繁体   English

在文件范围Struct C中修改的变量

[英]variable modified at file scope Struct C

Hi I get compile error when I try to assign the struct with array size of global variable which user will input in command line, then pass value back to variable that was declared globally. 嗨,当我尝试为结构分配全局变量的数组大小时会出现编译错误,用户将在命令行中输入该值,然后将值传递回全局声明的变量。

Here is just sample code 这只是示例代码

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

int Type;

struct list_el {
    int val;
} list1[Type];

struct list_el item;

int main() {
    Type = 10; //just sample
}

error 错误

variable modified at file scope Struct C 在文件范围Struct C中修改的变量

Is there anyway to fix the problem? 反正有解决此问题的方法吗? I need it to work globally. 我需要它在全球范围内运作。

Thank you so much. 非常感谢。

When declaring an array, you need a compile-time constant for the size, which Type isn't. 声明数组时,您需要一个编译时常量作为大小,而Type不是。

If you want to allocate during runtime, use pointers and malloc . 如果要在运行时分配,请使用指针和malloc


Well, actually you can have non-constant variables for array size, it's called variable-length arrays . 好吧,实际上您可以为数组大小设置非常数变量,这称为可变长度数组 The problem with using a global variable for the size is that global variables are initialized to zero, so what you are doing is actually creating an array with zero elements, but only if the variable Type is initialized before the array is created. 使用全局变量作为大小的问题是全局变量被初始化为零,因此您实际上是在创建一个包含零个元素的数组,但前提是要在创建数组之前初始化变量Type

Your code is not valid: you can't first declare an array using a variable as the size, and then assign to the variable. 您的代码无效:您不能使用变量作为大小声明数组, 然后再将其分配给变量。

Or, you can, but the array certainly won't magically change. 或者,您可以,但是阵列肯定不会发生神奇的变化。

Also, it's very strange to have the variable and array be global. 同样,让变量和数组为全局变量也很奇怪。

You should probably just do: 您可能应该这样做:

int main(void)
{
  int num;

  printf("enter size:\n");
  if(scanf("%d", &num) == 1 && num > 0)
  {
    struct list_el list1[num];

    /* here, work with the array. */
  }
}

Note that using a variable like this is a C99 feature. 请注意,使用这样的变量是C99功能。

If you want both the list and its magnitude Type to be global and dynamically allocated, you need to do just that: dynamically allocate . 如果您希望list及其大小Type都是全局的动态分配,则只需执行以下操作即可: 动态分配

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

int Type;

struct list_el {
    int val;
} *list;

int main() 
{
    Type = 10;

    list = malloc(Type * sizeof(*list));

}

That said, seriously question whether you need these globally. 也就是说, 严重质疑您是否在全球范围内需要这些。 If your compiler supports it you can use a local variable-length array (VLA). 如果您的编译器支持,则可以使用局部可变长度数组(VLA)。 It is a C99 optional feature, testable by checking if __STDC_NO_VLA__ is not defined. 它是一种C99可选功能,通过如果没有定义__STDC_NO_VLA__检查测试。

You could use alloca() in main to stack-allocate the memory. 您可以在main中使用alloca()对堆栈进行内存分配。

struct list_el {
  int n;
} *list;
static int Type;

int main(int argc, char** argv) {
  Type = argc;
  list = alloca(sizeof(struct list_el) * Type);

  // use list...
}

The memory will stay in-scope until main() returns. 内存将保持作用域内,直到main()返回。 I don't know what happens in an atexit() callback. 我不知道atexit()回调中会发生什么。 That's an interesting question. 这是一个有趣的问题。

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

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