P
pozz
I want to declare a constant linked list in static way, through
initializers.
First I declare the struct, with a member that points to the same
struct (the next element in the list):
---
typedef struct LIST_s LIST;
struct LIST_s {
const int num;
const LIST *next;
};
---
Then I just declare all the elements, so I'll be able to use their
pointers later during definitions:
---
const LIST l1;
const LIST l2;
const LIST l3;
const LIST l4;
---
At last I define the elements:
---
const LIST l1 = { 1, &l2 };
const LIST l2 = { 1, &l3 };
const LIST l3 = { 2, &l4 };
const LIST l4 = { 3, NULL };
---
It seems work well, but there is a compiler warning for each
declaration of lx variable:
`const' symbol `l1' has no initializer
The only solution I found is to use the 'extern' keywork for those
variables, but I don't think this is a good solution. Indeed I know
'extern' is used to declare a variable defined in a different module,
but I have a single module for the list.
Consider that the example is very simple (it is a constant list of
integers that could be transformed in a constant array), but my real
struct has several members.
initializers.
First I declare the struct, with a member that points to the same
struct (the next element in the list):
---
typedef struct LIST_s LIST;
struct LIST_s {
const int num;
const LIST *next;
};
---
Then I just declare all the elements, so I'll be able to use their
pointers later during definitions:
---
const LIST l1;
const LIST l2;
const LIST l3;
const LIST l4;
---
At last I define the elements:
---
const LIST l1 = { 1, &l2 };
const LIST l2 = { 1, &l3 };
const LIST l3 = { 2, &l4 };
const LIST l4 = { 3, NULL };
---
It seems work well, but there is a compiler warning for each
declaration of lx variable:
`const' symbol `l1' has no initializer
The only solution I found is to use the 'extern' keywork for those
variables, but I don't think this is a good solution. Indeed I know
'extern' is used to declare a variable defined in a different module,
but I have a single module for the list.
Consider that the example is very simple (it is a constant list of
integers that could be transformed in a constant array), but my real
struct has several members.