code muncher
code muncher

Reputation: 1682

Global structure pointer initialization

Structure definition

struct list
{
  struct list **next, **prev;
}

core.c

//Global struct
struct list *threads = {&threads, &threads};  //Warnings here:

// warning: initialization from incompatible pointer type
// warning: excess elements in scalar initializer
// warning: (near initialization for 'threads')

PS: I don't have a main function in this file. And this has to be global.

Upvotes: 2

Views: 1565

Answers (1)

Kevin Reid
Kevin Reid

Reputation: 43753

You need to initialize the pointer-to-struct-list variable threads with a pointer to a struct list. {&threads, &threads} is not a pointer to a struct list, but it could be a struct list.

In order to define an actual structure instance and obtain a pointer to it, you can use a compound literal and take its address:

struct list *threads = &((struct list){&threads, &threads});

(Note: compound literals ((type){initializer}) are a C99 feature; some compilers which haven't caught up to a 13-year-old standard may reject it.)

Upvotes: 3

Related Questions