EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

inizializing static const structures

Started by Mastupristi January 25, 2005
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?
On 25 Jan 2005 15:03:53 +0100, Mastupristi <cialdi_NO_SP@AM_gmail.com> 
wrote:

> 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" };
Hint: any good book on C language describes the difference between declaration and definition. HTH, Vadim
Mastupristi <cialdi_NO_SP@am_gmail.com> wrote:

> but this doesn't work. I obtain a "redefined" error on the last 3 lines.
You shouldn't. Looks like the compiler doesn't know how to handle tentative definitions.
> How can I initialize this list?
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.
On Tue, 25 Jan 2005 14:23:01 -0000
Vadim Borshchev <vadim.borshchev@127.0.0.1> wrote:

> > 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" }; > > Hint: any good book on C language describes the difference between > declaration and definition.
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?
On 25 Jan 2005 15:52:23 +0100, Mastupristi <cialdi_NO_SP@AM_gmail.com> 
wrote:

> 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.
You are right, and I was wrong.
> note that I tried the same code with gcc and it works fine.
It works in armcc and clarm as well. Your compiler seems to have a bug. Vadim
Mastupristi <cialdi_NO_SP@AM_gmail.com> wrote in 
news:20050125150026.00005b95.cialdi_NO_SP@AM_gmail.com:

> 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 >
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
On 25 Jan 2005 16:00:47 GMT
Richard <RichardRapier@netscape.net> wrote:

> 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" };
this was the first try. It doesn't work. thanks -- Mastupristi?
On 25 Jan 2005 15:03:53 +0100, Mastupristi <cialdi_NO_SP@AM_gmail.com>
wrote:

>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?
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.)
On Tue, 25 Jan 2005 16:22:18 GMT, no-one@dont-mail-me.com (Robert
Scott) wrote:

[...]
>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" };
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.
iddw@hotmail.com wrote:
>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.
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 ]

The 2024 Embedded Online Conference