Thread: Changing a variable to a constant.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Changing a variable to a constant.

    Hi,
    I have a question about changing a variable to a constant.

    My problem arrises using Texas Instruments' CCS3.1
    compiler. The following boiled down code builds finely in Microsofts VC6, but not in CCS:

    Code:
    void main(){
    	 const int bufsize = 2;
    	 int splitbuf[bufsize]; //line 5.
    }
    In CCS I get the following error message:

    "arrTest.c", line 5: error: expression must have a constant value

    Does anyone have a suggestion for a workaround, that will make it possible for me to change a variable into a constant so my program will compile in CCS?

    Best regards, Esben.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    The const keyword actually means "read only" in C. For true constants, C programmers usually use #define's - constant symbol, or, you could hard-code a constant value into your source code but the former is more used, for good reasons.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Hi xeddiex,
    thanks for your reply. I am aware of the #define option. I guess my problem needs a bit more specification:

    My code looks more simular to the following:

    Code:
    #define BUFSIZE(x,y) ((x)+(y))
    void main(){
    	const int x = 2;
    	const int y = 3;
    	const int bufsize = BUFSIZE(x, y);   
    	int splitbuf[bufsize]; 
    }
    It will work in VC6 if I remember to declare all the integers as const, but CCS ...buhuu... still sees 'bufsize' as a variable.

    Provided this new information, does anyone have an idea how to make my bufsize constant?

    Best regards, Esben.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    I think you're compiling in C++. Make sure your file extension ends in .c, not .cpp.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    #define X 2
    #define Y 3
    int splitbuf[BUFSIZE(X,Y)];

    > It will work in VC6 if I remember to declare all the integers as const
    This is what happens when you learn an implementation rather than the language.
    When you move to another compiler, it's always the same old "but it works on...." questions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    DOH! Thanks for the enlightenment.

    So now the question is if there is a bug in the original open source c code, that is not noticed by the MSVC... Guess I gotta look into that.

    Thanks again!
    So long.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So now the question is if there is a bug in the original open source c code
    There's a pretty good chance (the void main isn't a good sign in itself).

    Compiling with a new compiler not only shows up all the compiler extensions which were previously used, it's also likely to show up various memory problems which just happened to go unnoticed in the old system, but due to different alignment, ordering and initialisation state cause all sorts of problems in the new system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  2. Changing a const variable?
    By jw232 in forum C++ Programming
    Replies: 14
    Last Post: 09-01-2008, 09:53 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Changing variable
    By Mithoric in forum Windows Programming
    Replies: 4
    Last Post: 03-30-2004, 09:45 PM