inizializing static const structures

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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?

Re: inizializing static const structures

Quoted text here. Click to load it

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

HTH,

   Vadim

Re: inizializing static const structures
On Tue, 25 Jan 2005 14:23:01 -0000

Quoted text here. Click to load it
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?

Re: inizializing static const structures

Quoted text here. Click to load it

You are right, and I was wrong.

Quoted text here. Click to load it

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

   Vadim

Re: inizializing static const structures

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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

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

Re: inizializing static const structures

Quoted text here. Click to load it
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

Re: inizializing static const structures
On 25 Jan 2005 16:00:47 GMT

Quoted text here. Click to load it
this was the first try. It doesn't work.

thanks

--
Mastupristi?

Re: inizializing static const structures

Quoted text here. Click to load it

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

Re: inizializing static const structures
On Tue, 25 Jan 2005 16:22:18 GMT, no-one@dont-mail-me.com (Robert

[...]
Quoted text here. Click to load it

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.

Re: inizializing static const structures
Quoted text here. Click to load it

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 ]

Re: inizializing static const structures
Quoted text here. Click to load it
... snip ...
Quoted text here. Click to load it

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
We've slightly trimmed the long signature. Click to see the full one.
Re: inizializing static const structures

Quoted text here. Click to load it

C is has all the forward declarations it needs.

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

Re: inizializing static const structures
On Tue, 25 Jan 2005 18:30:34 -0500, Roberto Waltman

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

Not C, just the OP's compiler.

Regards,

                               -=Dave
--
Change is inevitable, progress is not.

Site Timeline