2

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.

1 Answer 1

3

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Kevin, one more question, next to struct initialization, I have something like void* vp_threads = threads; // if I write &threads, this's fine but without '&' its giving me error 'initializer element is not constant' Why so? any idea?
I don't know, sorry. Those are two different pointers, of course. If you need help on that you should ask a separate question, and give more details on what you need to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.