[Since there’s no specific C group, I’m posting this here]
Hi,
Consider the following peace of code:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
TIA,
rick
Previously, Rick Lake wrote in qdn.public.qnx4:
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
Watcom is ANSI C. gcc is a super-set of ANSI C, try -Wall --pedantic and see
what it says, out of curiosity.
I just checked my K&R, and the expressions inside a brace-enclosed
initializer list for an auto variable must be constant.
Sam
–
Sam Roberts (sam@cogent.ca), Cogent Real-Time Systems (www.cogent.ca)
Rick Lake <rwlake@anp.nl> wrote:
[Since there’s no specific C group, I’m posting this here]
Hi,
Consider the following peace of code:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
K&R 2nd edition (ANSI C) on page 128 says,
A structure can be initialized by following its definition with a
list of initializers, each a constant expression, for the members:
So, gcc is letting you get away with something that is non-ANSI, and
Watcom isn’t.
-David
Hi Rick,
Give your struct a constructor and use it like:
f()
{
struct _ST
{
int a, b;
_ST( int a2=0, int b2=0 ) : a(a2), b(b2) {};
} st( initfun(), 0 );
}
Rick Lake <rwlake@anp.nl> wrote in message news:39874914.6BEB7F97@anp.nl…
[Since there’s no specific C group, I’m posting this here]
Hi,
Consider the following peace of code:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
TIA,
rick
David Gibbs wrote:
Rick Lake <> rwlake@anp.nl> > wrote:
[Since there’s no specific C group, I’m posting this here]
Hi,
Consider the following peace of code:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
K&R 2nd edition (ANSI C) on page 128 says,
A structure can be initialized by following its definition with a
list of initializers, each a constant expression, for the members:
I don’t have a K&R book, but I take it that they’re (also) referring to
automatic variables here…
So, gcc is letting you get away with something that is non-ANSI, and
Watcom isn’t.
And that’s the way it should be This isn’t the first time I had my
Watcom detecting errors that slipped by gcc.
-David
Hi Bill,
Actually I was speaking C (I believe this is C++?) Nice solution, tho…
Incidentally, the example I gave is representative for code in the
latest PD libftp package.
Bill at Sierra Design wrote:
Hi Rick,
Give your struct a constructor and use it like:
f()
{
struct _ST
{
int a, b;
_ST( int a2=0, int b2=0 ) : a(a2), b(b2) {};
} st( initfun(), 0 );
}
Rick Lake <> rwlake@anp.nl> > wrote in message news:> 39874914.6BEB7F97@anp.nl> …
[Since there’s no specific C group, I’m posting this here]
Hi,
Consider the following peace of code:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
TIA,
rick
It says:
“initializer element for `st.a’ is not computable at load time”
thanks everyone for your replies
rick
Sam Roberts wrote:
Previously, Rick Lake wrote in qdn.public.qnx4:
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
Watcom is ANSI C. gcc is a super-set of ANSI C, try -Wall --pedantic and see
what it says, out of curiosity.
I just checked my K&R, and the expressions inside a brace-enclosed
initializer list for an auto variable must be constant.
Sam
–
Sam Roberts (> sam@cogent.ca> ), Cogent Real-Time Systems (> www.cogent.ca> )
Oh!
I forgot that still exists. I just automatically think C++.
If C, why not just:
f()
{
struct { int a, b;} st;
st.a = initfun(), st.b = 0;
…
}
Rick Lake <rwlake@anp.nl> wrote in message news:39887839.2BAE970F@anp.nl…
Hi Bill,
Actually I was speaking C (I believe this is C++?) Nice solution, tho…
That example was just to demonstrate a (somewhat larger) struct in a
package. I had already chosen your suggestion below. But I was just
compiler-curious…
Bill at Sierra Design wrote:
Oh!
I forgot that still exists. I just automatically think C++.
If C, why not just:
f()
{
struct { int a, b;} st;
st.a = initfun(), st.b = 0;
…
}
Rick Lake <> rwlake@anp.nl> > wrote in message news:> 39887839.2BAE970F@anp.nl> …
Hi Bill,
Actually I was speaking C (I believe this is C++?) Nice solution, tho…
Rick Lake <rwlake@anp.nl> writes:
…
extern initfun(); // some arbitrary function
…
f()
{
struct { int a, b;} st = {initfun(), 0};
…
}
Watcom complains that the expression “initfun()” should be constant,
whereas gcc accepts this. I can’t find in the Watcom language ref manual
where it states that this isn’t allowed. Simple typed vars don’t have
this problem.
Have I overlooked anything?
You are not supposed to include the () in the initializer. Use
struct { int a, b;} st = {initfun, 0};
Andrew
Andrew Thomas, President, Cogent Real-Time Systems Inc.
2430 Meadowpine Boulevard, Suite 105, Mississauga, Ontario, Canada L5N 6S2
Email: andrew@cogent.ca WWW: http://www.cogent.ca
Andrew Thomas <andrew@cogent.ca> writes:
You are not supposed to include the () in the initializer. Use
struct { int a, b;} st = {initfun, 0};
Doh! Forget this posting. I mis-interpreted what your code is trying
to do.
Andrew
Andrew Thomas, President, Cogent Real-Time Systems Inc.
2430 Meadowpine Boulevard, Suite 105, Mississauga, Ontario, Canada L5N 6S2
Email: andrew@cogent.ca WWW: http://www.cogent.ca