0

I'm trying to declare a dummy head* as global variable but the IDE is giving this error:

initializer element is not constant

How could I fix that?

typedef struct MyNode{
    struct MyNode *next;
    int value;
}Node;

//Declare a global variable
Node *head = malloc(sizeof(Node));
head->next = NULL;
Jongware
  • 22,200
  • 8
  • 54
  • 100
Nikola
  • 23
  • 1
  • 6
  • Instead let the dummy be `Node head; head.next = NULL;` and then use `&head` where the code previously used `head`. – chux - Reinstate Monica Oct 30 '15 at 21:08
  • IDEs do not issue this kind of errors. What compiler is this? – Jongware Oct 30 '15 at 21:09
  • 1
    Please clarify what your constraints and requirements are. The obvious thing to do is just to declare the `head` variable as a global but then do the initialisation within a function. – kaylum Oct 30 '15 at 21:10
  • @kaylum, but how? How to declare the head variable as a global but then do the initialisation within a function? – Nikola Oct 30 '15 at 21:12
  • Yes, `head` can be initialised to `NULL`, since `NULL` is a (macro that expands to) a constant expression. – Peter Oct 30 '15 at 21:24
  • @Peter: I was asking whether it makes sense in his app. Does he need a pointer? – Karoly Horvath Oct 30 '15 at 21:25
  • A better question is whether he needs a global. – Peter Oct 30 '15 at 22:07
  • @Peter: That's a good question. "better"? That's highly subjective... – Karoly Horvath Oct 31 '15 at 14:13
  • Well, no, in this case it is not subjective. If an answer to question A can mean it is not necessary to even ask question B, then question A is a better question by many measures ( (more widely applicable, more general, more enduring, etc). A carefully considered answer on whether a global is needed can completely eliminate the question of whether a pointer is needed, which in turn can eliminate the problems of initialising a pointer. – Peter Oct 31 '15 at 22:02

4 Answers4

2

The obvious thing to do is just to declare the head variable as a global but then do the initialisation within a function.

typedef struct MyNode{
    struct MyNode *next;
    int value;
} Node;

Node *head;

int main (int argc, char **argv)
{
    head = malloc(sizeof *head);
    if (!head) {
        /* error */
        exit(-1);
    }

    head->next = NULL;

    return 0;
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
1

You cannot have assignments outside functions. Here you find a complete discussion: Structure member assignment causing syntax error when not inside a function

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31
1

The C11 standard states in section 6.7.9 part 4:

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

Your pointer head is being initialized with a function call, which is not a constant expression. The only way to initialize it with a function call is to declare it inside of a function (however, this will not work if it's declared with static due to the above restriction). Alternatively, you can just let the pointer be initialized by default to NULL, and then assign it with malloc() later on.

doppelheathen
  • 388
  • 4
  • 8
0

From the C99 standard:

6.7.8 Initialization

Constraints

4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

Hence,

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

is not allowed. You can initialize head with a constant expression, such as NULL.

Node *head = NULL;

Of course, statements like:

head->next = NULL;

are not allowed outside functions. Hence, proper initialization of head has to be done in a function. One way to do that would be:

Node* head = NULL;

Node* createNode()
{
   Node* node = malloc(sizeof(*node));
   node->value = 0;
   node->next = NULL;
   return node;
}

int main()
{
   head = createNode();
   ...
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270