Hi,
I have problems to initialize a static struct. Here is the meaningful
part of the code:
int main()
{
int pA = -100;
struct globalMixed4 {
int a;
int *b;
};
static struct globalMixed4 globMix4 =
{ 200, &pA }; // my line 70
When try to compile, I get the gcc error message:
struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA.
How can I make it const?
And in general, why does the compiler expect a const initializer?
Thank you.
Chris 9 23293
Christian Christmann wrote On 03/23/06 17:29,: Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const?
static int pA = -100;
And in general, why does the compiler expect a const initializer?
Because static variables exist throughout the
entire life of the program, their initialization must
take place before the program starts executing (that
is, before main() is called). Thus, the initialization
can only use expressions that can be evaluated before
execution starts. The address of an `auto' variable
like your original `pA' is not known until the block
that contains it is entered at run-time, hence that
address can't be determined before run-time.
-- Er*********@sun.com
Christian Christmann wrote: Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const? And in general, why does the compiler expect a const initializer?
Objects with static duration are initialised
prior to program start, so they must be initialised with
values that are known at compile time, in other words,
they must be initialised with constant expressions.
The addresses of automatic variables aren't known
at compile time, main might be recursive.
The addresses of static variables and external variables
(aka "globals") are constant expressions.
--
pete
Eric Sosman schrieb: Christian Christmann wrote On 03/23/06 17:29,:
Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const?
static int pA = -100;
Nit: This is not exactly the same as int pA = -100; if it
comes to using the value of pA in other initialisers.
In C89, we'd need
static int pA = -100;
.... /* declaration list */
pA = -100;
/* statement list */
pA = -100;
/* return if necessary */
and in C99,
static int pA;
pA = -100;
....
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair wrote On 03/23/06 17:57,: Eric Sosman schrieb:
Christian Christmann wrote On 03/23/06 17:29,:
Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const?
static int pA = -100;
Nit: This is not exactly the same as int pA = -100; if it comes to using the value of pA in other initialisers.
Right: `static' is not `auto'. I was addressing
the question of how to make `&pA' constant, not the
value stored in `pA' itself. Naturally, the change
has semantic effects beyond the mere address-constness.
-- Er*********@sun.com
On Thu, 23 Mar 2006 23:29:48 +0100, Christian Christmann
<pl*****@yahoo.de> wrote: Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const? And in general, why does the compiler expect a const initializer?
It requires it only for static variables. And even though the address
of pA will not change during execution of your program, it is not a
constant, just like
const int b =10;
does not make b a constant and
char x[b];
will produce a similar diagnostic on a non-C99 system.
If you make pA static, the problem should go away. For some reason,
the address of a static variable is a constant.
Remove del for email
Christian Christmann дµÀ£º Hi,
I have problems to initialize a static struct. Here is the meaningful part of the code:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const? And in general, why does the compiler expect a const initializer? Thank you. Chris
This is my first join in this area , I 'm a chinese , Would you
mind we are created a good friendship.
Michael Mair <Mi**********@invalid.invalid> wrote: Eric Sosman schrieb: Christian Christmann wrote On 03/23/06 17:29,:
int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const? static int pA = -100;
Nit: This is not exactly the same as int pA = -100; if it comes to using the value of pA in other initialisers.
Could you, please, explain what exactly the problem is?
In C89, we'd need static int pA = -100;
Assuming a block scope, above initialization would be
illegal, according to ansi_c draft. .... /* declaration list */ pA = -100; /* statement list */ pA = -100; /* return if necessary */ and in C99, static int pA; pA = -100; ....
I can't see what the difference is between C89 and C99.
--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
S.Tobias schrieb: Michael Mair <Mi**********@invalid.invalid> wrote:Eric Sosman schrieb:Christian Christmann wrote On 03/23/06 17:29,:int main() { int pA = -100;
struct globalMixed4 { int a; int *b; };
static struct globalMixed4 globMix4 = { 200, &pA }; // my line 70
When try to compile, I get the gcc error message: struct.c:70: error: initializer element is not constant
The non-constant initializer element is &pA. How can I make it const?
static int pA = -100;
Nit: This is not exactly the same as int pA = -100; if it comes to using the value of pA in other initialisers.
Could you, please, explain what exactly the problem is?
Of course. Consider
#include <stdio.h>
int ctr = 5;
int main (void)
{
int pA = -100;
int pB = pA * 2;
static struct { int a; int *b; } globMix4 = { 200, &pA };
--pA; --ctr; /* o */
printf("%d\n", pA);
if (ctr != 0)
(void) main();
return 0;
}
This will not compile.
If we assume that globMix4 could be initialised that way, then
we have pB == -200 at initialisation and pA == -101 at the last
time "o" is reached. For
#include <stdio.h>
int ctr = 5;
int main (void)
{
static int pA = -100;
int pB = pA * 2;
static struct { int a; int *b; } globMix4 = { 200, &pA };
--pA; --ctr; /* o */
printf("%d\n", pA);
if (ctr != 0)
(void) main();
return 0;
}
we have pB == -200, -202, ..., -208 and pA = -105 at the last time "o"
is reached. In C89, we'd need static int pA = -100;
Assuming a block scope, above initialization would be illegal, according to ansi_c draft.
How so? .... /* declaration list */ pA = -100; /* statement list */ pA = -100; /* return if necessary */
#include <stdio.h>
int ctr = 5;
int main (void)
{
static int pA = -100;
int pB = pA * 2;
static struct { int a; int *b; } globMix4 = { 200, &pA };
--pA; --ctr; /* o */
printf("%d\n", pA);
pA = -100; /* reset to effect of auto for next main() call */
if (ctr != 0)
(void) main();
return 0;
}
If you have a function not called recursively with an internal
state then resetting the value to the initialiser before returning
would have the desired effect.
and in C99, static int pA; pA = -100; ....
I can't see what the difference is between C89 and C99.
In C99, you can make sure that pA really behaves as if initialised
to "-100" every time:
#include <stdio.h>
int ctr = 5;
int main (void)
{
static int pA = -100;
pA = -100;
int pB = pA * 2;
static struct { int a; int *b; } globMix4 = { 200, &pA };
--pA; --ctr; /* o */
printf("%d\n", pA);
if (ctr != 0)
(void) main();
return 0;
}
The C89 version of course works the same under C99 but has
the disadvantage that it may be necessary to reset pA in
more than one place -- or that it is not at all possible to
reset pA always correctly without introducing another variable.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Avi Uziel |
last post by:
Hi All,
I'm writing a Windows DLL which contain some utility classes.
One of my classes is Singleton, therefore contain some static members.
I'm using VC6 and linking to the DLL statically.
...
|
by: mark fine |
last post by:
////////////// snippet ////////////////
struct t {
static struct { } a;
};
struct { } t::a;
int main(void)
{
|
by: Bill |
last post by:
I would like to create a static array of classes (or structs) to be used in
populating name/value pairs in various WebForm drop down list boxes, but am
not quite sure of the construct (or rather to...
|
by: AnandRaj |
last post by:
Hi guys,
I have a few doubts in C.
1. Why static declartions are not allowed inside structs?
eg
struct a {
static int i;
};
Throws an error ..
|
by: Bart Goeman |
last post by:
Hi,
I have a question about how to put redundant information in data
structures, initialized at compile time. This is often necessary
for performance reasons and can't be done at run time (data...
|
by: ccdrbrg |
last post by:
I am trying to initialize an arrary of
pointers to structs with constants.
Sample code:
struct mystruct {
char *text;
int number;
};
|
by: Andreas Boehm |
last post by:
Hi *.*,
does the standard meanwhile define something about initializing
variables by the compiler? I think, it is a side-effect of the OS used,
if undefined global (static) variables are...
|
by: Eric Johannsen |
last post by:
Hi,
My C# code is calling VB6 code, which expects all (fixed-length) strings to
be padded with spaces. The strings are contained with a struct, something
like this (attributes to simulate...
|
by: Jai Prabhu |
last post by:
Hi All,
Consider the following piece of code:
void func (void)
{
static unsigned char arr = "\x00\xAA\xBB";
fprintf (stderr, "0x%x\n", arr);
fprintf (stderr, "0x%x\n", arr);
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |