This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc bug? or how can I learn to get rid of '#define' and love 'const'?


I have a very simple helloworld to try global constants:

////////////// start of code //////////////

//
// const.c
//
// test const globals

#include <stdio.h>

const int firstglobalint = 100;
const int secondglobalint = firstglobalint;

int
main()
{
    const int firstlocalint = 100;
    const int secondlocalint = firstlocalint;

    printf("hello world with 1st %d and 2nd %d!\n", firstlocalint,
secondlocalint);

    return 0;
}

//////////////// end of code ////////////////

when try to compile this piece of code, the global variable
'secondglobalint' causes the error:

const.c:9: initializer element is not constant

(gcc 2.91.66)

however, local variable 'secondlocalint' never had this problem.

Could it be a gcc bug? any workaround?

The situation I want use this code is that I have some code that used
many of

#define      MYVALUE        10    // in myoldheader.h

and I want to change them to

static const int MYVALUE = 10;   // in mynewheader.h

this 1) helps datatype checking when compiling, and 2) helps when I
debug the code because MYVALUE now is a variable so I can see its value
on fly.

then of course I have some other ones like this:

#define     HERVALUE      (MYVALUE+10)

but

static const int HERVALUE = MYVALUE + 10;

will cause syntax problem when compiling.

so what's right way to do this?


Sent via Deja.com http://www.deja.com/
Before you buy.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]