inizializing static const structures

Jan 25, 2005 12 Replies

We use IAR 78000 C-Compiler V3.21A/386. I want to initialize a static const list of structures as follow:



typedef struct rec { const struct rec *succ; const struct rec *prec; char *str; }Node_t;



static const Node_t n1; static const Node_t n2; static const Node_t n3;



static const Node_t n1 = { &n2, &n3, "Node 1" }; static const Node_t n2 = { &n3, &n1, "Node 2" }; static const Node_t n3 = { &n1, &n2, "Node 3" };


but this doesn't work. I obtain a "redefined" error on the last 3 lines. How can I initialize this list? It is mandatory that all structures are const, and I prefer if they are also static.



thanks


Mastupristi?

Hint: any good book on C language describes the difference between declaration and definition.

HTH,

Vadim

You shouldn't. Looks like the compiler doesn't know how to handle tentative definitions.

Only by getting a that compiler bug fixed, or a change of tools, I suspect.

Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.

thanks professor, but this hint doesn't help me. I know the difference between declaration and definition.

As you can see in definition of n1 is used &n2 and &n3, so the declaration of n2 and n3 must be placed before this line. If I am wrong tell me.

But if I place the declaration before, the compiler give the "redeclaration error".

note that I tried the same code with gcc and it works fine. Unfortunately gcc cannot compile code for NEC 78F9026A.

thanks

Mastupristi?

You are right, and I was wrong.

It works in armcc and clarm as well. Your compiler seems to have a bug.

Vadim

Mastupristi wrote in news:20050125150026.00005b95.cialdi_NO_SP@AM_gmail.com:

lines.

To make it work with your buggy compiler, try removing the tentative declarations.

typedef struct rec { const struct rec *succ; const struct rec *prec; char *str; }Node_t;

static const Node_t n1 = { &n2, &n3, "Node 1" }; static const Node_t n2 = { &n3, &n1, "Node 2" }; static const Node_t n3 = { &n1, &n2, "Node 3" };

Richard

this was the first try. It doesn't work.

thanks

Mastupristi?

If you can afford to let n1, n2, and n3 become global, then try this:

extern const Node_t n1,n2,n3;

const Node_t n1 = { &n2, &n3, "Node 1" }; const Node_t n2 = { &n3, &n1, "Node 2" }; const Node_t n3 = { &n1, &n2, "Node 3" };

-Robert Scott Ypsilanti, Michigan (Reply through this forum, not by direct e-mail to me, as automatic reply address is fake.)

[...]

If you can't, I believe the following is supposed to work as well:

extern const Node_t n1,n2,n3;

static const Node_t n1 = { &n2, &n3, "Node 1" }; static const Node_t n2 = { &n3, &n1, "Node 2" }; static const Node_t n3 = { &n1, &n2, "Node 3" };

This is apparently how things were done before the standard, and the standard supports the old code.

Regards,

-=Dave

Change is inevitable, progress is not.

gcc accepts that, unless you are using the "-pedantic" switch. Then it produces warnings, (not errors,) about "static declaration following non-static".

What C is missing is forward declarations, a la Pascal ...

Roberto Waltman.

[ Please reply to the group, return address is invalid ]

... snip ...

It has them, except they are called prototypes. Neither language can forward declare a variable. Both can declare pointers to undefined types. Pascal simply says that all pointers occupy the same storage space. C uses the incomplete type.

"If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson

C is has all the forward declarations it needs.

Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.

Actually, I mis-spoke. Change the first line to

const Node_t n1,n2,n3;

(i.e., remove the "extern" and leave off the "static") and it should be legal.

Not C, just the OP's compiler.

Regards,

-=Dave

Change is inevitable, progress is not.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required