C语言 分配内存时出现错误“初始化元素不是常量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13620654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:36:34  来源:igfitidea点击:

Error "initializer element is not constant" when allocate the memory

c

提问by Angus

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 
  4 typedef struct node_t{
  5     int i;
  6     struct node_t* link;
  7 }node;
  8 
  9 node* head = (node *)malloc(sizeof(node));
 10 
 11 if(head == NULL){
 12     printf("\n malloc for head node failed! \n");
 13 }
 14 
 15 int main(){
 16     int i = 10;
 17     node* temp = NULL;
 18     temp = (node *)malloc(sizeof(node));
 19     if(temp == NULL){
 20         printf("\n malloc for temp node failed! \n");
 21     }
 22     else{
 23         while(i<=10){
 24             ;
 25         }
 26     }
 27     return 0;
 28 } 

compilation error:

编译错误:

linked.c:9:1: error: initializer element is not constant
linked.c:11:1: error: expected identifier or a?(a? before a?ifa?

I'm trying a simple linked list programme. It's not fully completed. I'm getting a compilation error. Couldn't understand why this happened.

我正在尝试一个简单的链表程序。它没有完全完成。我收到编译错误。无法理解为什么会这样。

回答by Jerry Coffin

Since you're defining headas a global, its initializer needs to be a constant--basically, the compiler/linker should be able to allocate space for it in the executable, write the initializer into the space, and be done. There's no provision for calling mallocas you've done above during initialization--you'll need to do that inside of main(or something you call from main).

由于您定义head为全局变量,它的初始化程序需要是一个常量——基本上,编译器/链接器应该能够在可执行文件中为其分配空间,将初始化程序写入空间,然后完成。没有malloc像上面那样在初始化期间调用的规定——您需要在内部main(或从 调用的东西main)执行此操作。

#include <stdlib.h>

void init() { 
    head = malloc(sizeof(node));
}

int main() { 
    init();
    // ...
}

In this case, the code you have in mainnever actually uses headthough, so you may be able to skip all of the above without a problem.

在这种情况下,您所拥有的代码main实际上从未使用过head,因此您可以毫无问题地跳过上述所有内容。

回答by Omkant

 9  node* head = (node *)malloc(sizeof(node));
 10 
 11 if(head == NULL){
 12     printf("\n malloc for head node failed! \n");
 13 }

These lines are not possible outside the main()because any function call or executable should be inside the main()function or any function called from main.

这些行在外部是不可能的,main()因为任何函数调用或可执行文件都应该在main()函数或从 main 调用的任何函数内部。

For linked.c:9:1: error: initializer element is not constant

为了 linked.c:9:1: error: initializer element is not constant

Only function definitions or any global initialization is possible outside main()but initializer must be constant expression`.

只有函数定义或任何全局初始化是可能的,main()但初始化器必须是常量表达式`。

You can declare the headas global but initialization is wrong.

您可以将其声明head为全局,但初始化是错误的。

Do it like this :

像这样做 :

node * head =NULL // here initialization using constant expression


void function () // any function 
{
 head = malloc(sizeof(node));
}

For linked.c:11:1: error: expected identifier,

对于linked.c:11:1: error: expected identifier

ifstatement cannot be outside any function. In your case , put these lines inside main and problem solved

if语句不能在任何函数之外。在你的情况下,把这些行放在 main 中,问题就解决了

回答by StoryTeller - Unslander Monica

headis a global varibale. Global and static varibales must be initialized by constant expressions, i.e. literals. so you can't do

head是全局变量。全局变量和静态变量必须由常量表达式(即文字)初始化。所以你不能做

node* head = (node *)malloc(sizeof(node));

回答by Tushar Kanani

You can't use malloc() in global scope. or you can do like follow

您不能在全局范围内使用 malloc()。或者你可以像关注

#include<stdio.h>
#include<malloc.h>
  :
node* head
  :
  :
int main(){
  :
  :
head = (node *)malloc(sizeof(node));
  :
  :
}