Thread: Why make a constant a constant again?

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Why make a constant a constant again?

    The following is from a FreeBSD modem dialup program.

    in ward.h
    Code:
    #define SPEAKER_ON      "M1"
    #define SPEAKER_OFF     "M0"
    #define SPKR_VOL_0      "M1L0"
    #define SPKR_VOL_1      "M1L1"
    #define SPKR_VOL_2      "M1L2"
    #define SPKR_VOL_3      "M1L3"
    And in modem.c
    Code:
    const char *Vol [] = { 
            SPEAKER_OFF, SPKR_VOL_0, SPKR_VOL_1, SPKR_VOL_2, SPKR_VOL_3
    };
    [[email protected] award-1.0]# grep -iR SPEAKER_OFF /tmp/award-1.0
    /tmp/award-1.0/modem.c: SPEAKER_OFF, SPKR_VOL_0, SPKR_VOL_1, SPKR_VOL_2, SPKR_VOL_3
    /tmp/award-1.0/ward.h:#define SPEAKER_OFF "M0"

    #define already makes stuff like SPEAKER_OFF constant. Why make it constant again in modem.c? Also, why not use these constants?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, the "constants" are constant.
    But this
    Code:
    const char *Vol []
    says to the compiler that "We're not about to change the contents of the elements in this array" - so if the compiler wishes to be clever about it (e.g. "remembering" something across a function call), it doesn't need to worry about some code going changing the strings somewhere else.

    Of course, if you then start modifying the strings, the compiler will give you an error - at least if the compiler actually understands what's going on.

    So there's certainly very good reason to do things this way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If the 'const' wasn't there then it would be saying that Vol is an array of pointers to strings that can be modified. However it actually only contains string literals, which must NOT be modified, hence the const for protection.

    In fact, I would have put an extra const in there as well:
    Code:
    const char * const Vol []
    This would stop the array elements themselves from being changed to point to different string literals later on.
    Last edited by iMalc; 11-03-2007 at 05:32 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I think part of the confusion stems from what #define is really doing.
    #define doesn't create constants as you might be thinking of them. #define will just create a macro, which is essentially text substitution. So if you have the following:
    Code:
    #define FOO "foo"
    const char *a[] = { FOO };
    You'll actually wind up with the following code:
    Code:
    const char *a[] = { "foo" };
    FOO isn't a variable at all, but something that's simply replaced. That explains why it acts like a constant:
    Code:
    #define FOO "foo"
    FOO = "bar";
    This doesn't work because it'd expand to: "foo" = "bar"; nonsense, clearly. But macros don't have a type, so they're not flagged as "constant". They expand to something that might have a type; in the instant case you see that they simply expand to a string literal, which has type array-of-char. No const! See the above posts for why you'd then want to use const yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM