const global vars 
Author Message
 const global vars

Hi! I already wrote on the 6th asking how to substitute #define pr?pro
arguments through global vars. You helped me a lot, thanx.

Ive created a new module with the corresponding
#define vars like this:

Quote:
> /*old header file*/
> #define EXONWEIGHT 1.0

to =>
 > /*globvars.h*/

Quote:
> extern float EXONWEIGHT;
> void getvars();

&

Quote:
> /*globvars.c*/
> float EXONWEIGHT;
> void getvars() {
>       EXONWEIGHT = 1.0;
> }

the source of the prog uses the var EXONWEIGHT like this:

Quote:
> float lastexonweight = 0.5*EXONWEIGHT;

the error message form the gcc 3.2 compiler is:

Quote:
> morgan.c:27: initializer element is not constant
> make: *** [morgan.o] Error 1

Ive changed the var to "const float EXONWEIGHT" but the error is the
same. Im not sure if its possible to substitute this kind of #define
vars just by change them to glob vars. But it would save a lot of work
to me if its possible in such a way

Thanx, Christian



Sat, 28 May 2005 22:40:52 GMT  
 const global vars

heidelberg.de:

Quote:
> Hi! I already wrote on the 6th asking how to substitute #define pr?pro
> arguments through global vars. You helped me a lot, thanx.

> Ive created a new module with the corresponding
> #define vars like this:

>> /*old header file*/
>> #define EXONWEIGHT 1.0

> to =>
> > /*globvars.h*/
>> extern float EXONWEIGHT;
>> void getvars();

> &
>> /*globvars.c*/
>> float EXONWEIGHT;
>> void getvars() {
>>       EXONWEIGHT = 1.0;
>> }

i don't see the point of getvars().

here is a minimal example:

/* g.h */
#ifndef G_H
#define G_H
extern float EXONWEIGHT;
#endif

/* v.c */
#include "g.h"

float EXONWEIGHT = 1.0;

/* g.c */
#include <stdio.h>

#include "g.h"

int main(void)
{
        float f = 0.5 * EXONWEIGHT;
        printf("f = %f\n", f);
        return 0;

Quote:
}

C:\Dload\misc>gcc g.c v.c -o g.exe -Wall

C:\Dload\misc>g
f = 0.500000
--
A. Sinan Unur

Remove dashes for address



Sat, 28 May 2005 23:45:34 GMT  
 const global vars

Quote:

> i don't see the point of getvars().

> here is a minimal example:

> /* g.h */
> #ifndef G_H
> #define G_H
> extern float EXONWEIGHT;
> #endif

> /* v.c */
> #include "g.h"

> float EXONWEIGHT = 1.0;

> /* g.c */
> #include <stdio.h>

> #include "g.h"

> int main(void)
> {
>         float f = 0.5 * EXONWEIGHT;
>         printf("f = %f\n", f);
>         return 0;
> }

> C:\Dload\misc>gcc g.c v.c -o g.exe -Wall

> C:\Dload\misc>g
> f = 0.500000

.. ok, getvars() is not necessary, I want it implement an input routine
later.
the problem is that there are also var calls in the global dekl. and
definition section.

/* any.c*/
#include <stdio.h>
#include "g.h"

int a = EXONWEIGHT*0.5;

int main(void) { ... }

it causes the error message ... EXONWEIGHT is not constant!



Sun, 29 May 2005 00:07:52 GMT  
 const global vars

heidelberg.de:

Quote:

>> i don't see the point of getvars().

>> here is a minimal example:

<snip>

Quote:
> .. ok, getvars() is not necessary, I want it implement an input routine
> later.
> the problem is that there are also var calls in the global dekl. and
> definition section.

> /* any.c*/
> #include <stdio.h>
> #include "g.h"

> int a = EXONWEIGHT*0.5;

> int main(void) { ... }

> it causes the error message ... EXONWEIGHT is not constant!

one solution would be to initialize all your global variables in the same
translation unit.

another possibility is:

int a;

static void init_unit_globals(void)
{
    a = EXONWEIGHT * 0.5;

Quote:
}

int main(void)
{
    init_unit_globals();
    /* ... */
    return 0;

Quote:
}

--
A. Sinan Unur

Remove dashes for address



Sun, 29 May 2005 00:24:10 GMT  
 const global vars


Quote:


> heidelberg.de:

> >> i don't see the point of getvars().

> >> here is a minimal example:

> <snip>

> > .. ok, getvars() is not necessary, I want it implement an input routine
> > later.
> > the problem is that there are also var calls in the global dekl. and
> > definition section.

That's going to be a problem, then.  You probably realize by now that your
original error message was caused by trying to evaluate an expression at
file scope which was not a compile-time constant (or, in standard-speak,
trying to initialize an object having static storage duration with
something which was not a constant expression or string literal).  That
is, when the compiler sees

    int f = 0.5 * 2;

it knows to initialize f to 1, which can be done at compile time, but when
it encounters

    /* const or not */ double EXONWEIGHT = 42.0;
    int f = 0.5 * EXONWEIGHT;

it cannot initialize f at compile time.  You need to put an assignment to
f somewhere in a function, or, even better, move the entire variable
definition into a function.

Quote:
> > /* any.c*/
> > #include <stdio.h>
> > #include "g.h"

> > int a = EXONWEIGHT*0.5;

> > int main(void) { ... }

> > it causes the error message ... EXONWEIGHT is not constant!

Yep.  "Constants aren't."

Quote:
> one solution would be to initialize all your global variables in the same
> translation unit.

This won't make the code correct, although a smart compiler like gcc may
have an extension that makes it work on your system.

Quote:
> another possibility is:

> int a;

> static void init_unit_globals(void)
> {
>     a = EXONWEIGHT * 0.5;
> }

> int main(void)
> {
>     init_unit_globals();
>     /* ... */
>     return 0;
> }

A much better possibility is:

int main(void)
{
    int a = EXONWEIGHT * 0.5;
    do_something_with_(a);
    return 0;

Quote:
}

HTH,
-Arthur


Sun, 29 May 2005 00:47:07 GMT  
 const global vars


Quote:

>> i don't see the point of getvars().

>> here is a minimal example:

>> /* g.h */
>> #ifndef G_H
>> #define G_H
>> extern float EXONWEIGHT;
>> #endif

>> /* v.c */
>> #include "g.h"

>> float EXONWEIGHT = 1.0;

>> /* g.c */
>> #include <stdio.h>

>> #include "g.h"

>> int main(void)
>> {
>>         float f = 0.5 * EXONWEIGHT;
>>         printf("f = %f\n", f);
>>         return 0;
>> }

>> C:\Dload\misc>gcc g.c v.c -o g.exe -Wall

>> C:\Dload\misc>g
>> f = 0.500000

> .. ok, getvars() is not necessary, I want it implement an input routine
> later.
> the problem is that there are also var calls in the global dekl. and
> definition section.

> /* any.c*/
> #include <stdio.h>
> #include "g.h"

> int a = EXONWEIGHT*0.5;

> int main(void) { ... }

> it causes the error message ... EXONWEIGHT is not constant!

Be cause it isn't! A const *variable* is not the same as a constant
expression which is required here. As a rule I don't ALLCAP const
variables, yes, const variables only those constants that you cannot take
the address of so I don't slip into this trap mistakenly. C just can't do
what you ask even though one would think it could. Your problem is
analogous to:

const int var = 12;

char arr[var]; /* Error, not a constant */

Do the asignment at run-time as A. Sinan Unar suggests.



Sun, 29 May 2005 01:11:38 GMT  
 const global vars


Quote:


>> one solution would be to initialize all your global variables in the
>> same translation unit.

> This won't make the code correct, although a smart compiler like gcc
> may have an extension that makes it work on your system.

Sorry for the misleading statement. It is wrong, and I wasn't really
thinking of any extension mechanism really.

Quote:
>> another possibility is:

>> int a;

>> static void init_unit_globals(void)
>> {
>>     a = EXONWEIGHT * 0.5;
>> }

>> int main(void)
>> {
>>     init_unit_globals();
>>     /* ... */
>>     return 0;
>> }

> A much better possibility is:

> int main(void)
> {
>     int a = EXONWEIGHT * 0.5;
>     do_something_with_(a);
>     return 0;
> }

clearly ... however, if the OP needs globals for some really good reason,
he can use the init function approach.

--
A. Sinan Unur

Remove dashes for address



Sun, 29 May 2005 04:45:08 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. global vars vs: local vars

2. global/static vars inside loops

3. Global vars versus pointers

4. how to access local and global vars in a function

5. global vs local vars

6. global/static vars inside loops

7. static global vars and functions

8. global Vars

9. Global vs. Local vars and .MAP

10. Accessing vars in global and module scope in ANSI C

11. Accessing vars in global and module scope in ANSI C

12. Accessing vars in global

 

 
Powered by phpBB® Forum Software