By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Initializing static structs

P: n/a
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


Mar 23 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a


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

Mar 23 '06 #2

P: n/a
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
Mar 23 '06 #3

P: n/a
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.
Mar 23 '06 #4

P: n/a


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

Mar 23 '06 #5

P: n/a
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
Mar 24 '06 #6

P: n/a

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


Mar 24 '06 #7

P: n/a
This is my first join in this area , I 'm a chinese , Would you
mind we are created a good friendship.

Mar 24 '06 #8

P: n/a
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`
Apr 11 '06 #9

P: n/a
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.
Apr 11 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.