Casting volatile and const variables

Discussion in 'C Programming' started by ramu, Oct 26, 2009.

  1. ramu

    ramu Guest

    Hi,
    I read somewhere in the web that const and volatile variable
    shoult not be type casted. Can you please tell me why shouldn't we
    type cast const and volatile variables?

    Regards
     
    ramu, Oct 26, 2009
    #1
    1. Advertising

  2. ramu <> writes:

    > I read somewhere in the web that const and volatile variable
    > shoult not be type casted. Can you please tell me why shouldn't we
    > type cast const and volatile variables?


    [Aside: I don't like using cast as a verb and I particularly dislike
    "type cast" as a verb because of its plain English meaning. A cast
    operator simple converts a value from one type to another, and it is
    the conversion that is important, not the operator that does it. In
    other words you can simply talk about "conversions" rather than "casts"
    and be using simpler English whilst covering a wider range of cases.
    Both unsigned int x = -1; and (unsigned int)-1 perform a conversion
    from signed to unsigned int, and it is the conversion that most
    people mean when they talk about "casting to unsigned". Of course,
    some conversions can only be done with a cast, but I still think the
    plain English term is better.]

    I think the text you read is misleading you. Maybe it said that you
    should not "cast away" the effect of the const or volatile type
    qualifiers? That may be sound advice, but only in the trivial sense
    that those qualifiers serve a purpose, and removing them will
    therefore prevent that purpose being served.

    Note that const and volatile only mean anything when applied to
    objects. Converting the value of a cont double object to, say, an int
    value is harmless and is not what people mean by "casting away" const.
    Given:

    const double pi = 3.14;
    printf("%d\n", (int)pi);

    the const removal is harmless. But given:

    const char *str = "pi";
    strcpy((char *)str, "3");

    The cast removes the qualifier const form _the object pointed to_ by
    "str", and is a clear sign of something very dangerous (and in this
    case plain wrong).

    --
    Ben.
     
    Ben Bacarisse, Oct 26, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    5
    Views:
    623
    Ulrich Eckhardt
    Jan 11, 2005
  2. Replies:
    11
    Views:
    1,149
  3. Javier
    Replies:
    2
    Views:
    618
    James Kanze
    Sep 4, 2007
  4. Alex Vinokur
    Replies:
    9
    Views:
    830
    James Kanze
    Oct 13, 2008
  5. Ravi

    Casting volatile and const variables

    Ravi, Sep 24, 2011, in forum: C Programming
    Replies:
    3
    Views:
    408
Loading...

Share This Page