C language: initializer element is not constant

Source: Internet
Author: User
static struct Queueptr * Header = (struct queueptr *) malloc (sizeof (struct queueptr));
Wrong in compile times: initializer element is not constant
Reason: Indicates that the initialization of global and static variables must be constant expressions in C99
Modify:
static struct Queueptr * Header = NULL;
Header = (struct queueptr *) malloc (sizeof (struct queueptr)));


Check out the other blogs and see that the following code will also report the same error:
Link: Why "initializer element is not constant" error appears
#include <stdio.h>  
int a = 1;  
int b = 2;  
int c = a+b;  
  
int main () {  
    printf ("C is%d\n", c);  
    return 0;  
}  
Similarly, the value of global variable C in the above code is determined at execution time. The initialization of a global variable is done at compile time, so it cannot be a variable whose value has not yet been determined.


(1) The initialization of global and static variables in C language needs to specify a constant, which cannot be a very variable expression;
(2) as a global variable, after compiling to determine its address, if the global variable is a structural body, then the structure of the individual members of the specific values to be determined. Because a global variable is stored in a static area, only a constant expression can determine the value in advance, rather than the specific run-time determination.
(3) Basis:
C99 Standard 6.7.8 Initialization 4th paragraph:
4 the expressions in a initializer for a, has static storage duration shall be constant expressions or St Ring literals.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.