const int return type

Discussion in 'C Programming' started by Cocon Cocon, Aug 4, 2011.

  1. Cocon Cocon

    Cocon Cocon Guest

    I see what 'const' does to global variables and pointer arguments. But
    how about return types?

    Consider:

    const int f() {
    ...
    }

    What does the 'const' change?

    Specifically, what would one have to put in for the '...' in order to
    get a compile-time error?
     
    Cocon Cocon, Aug 4, 2011
    #1
    1. Advertising

  2. Cocon Cocon <> writes:

    > I see what 'const' does to global variables and pointer arguments. But
    > how about return types?
    >
    > Consider:
    >
    > const int f() {
    > ...
    > }
    >
    > What does the 'const' change?


    Nothing. const is a property of objects not values and in C functions
    return values not objects. That's a simplification -- a more technical
    description is that function calls are not lvalue expressions.

    > Specifically, what would one have to put in for the '...' in order to
    > get a compile-time error?


    I'd have thought you would be likely to get a diagnostic just for having
    the const there though I don't think the C standard requires one.

    --
    Ben.
     
    Ben Bacarisse, Aug 4, 2011
    #2
    1. Advertising

  3. Ben Bacarisse <> writes:
    > Cocon Cocon <> writes:
    >> I see what 'const' does to global variables and pointer arguments. But
    >> how about return types?
    >>
    >> Consider:
    >>
    >> const int f() {
    >> ...
    >> }
    >>
    >> What does the 'const' change?

    >
    > Nothing. const is a property of objects not values and in C functions
    > return values not objects. That's a simplification -- a more technical
    > description is that function calls are not lvalue expressions.
    >
    >> Specifically, what would one have to put in for the '...' in order to
    >> get a compile-time error?

    >
    > I'd have thought you would be likely to get a diagnostic just for having
    > the const there though I don't think the C standard requires one.


    It doesn't, but gcc (with the right options) warns:

    warning: type qualifiers ignored on function return type

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 4, 2011
    #3
    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. Timo Freiberger
    Replies:
    3
    Views:
    975
    Bob Hairgrove
    Oct 30, 2004
  2. ThazKool
    Replies:
    1
    Views:
    457
  3. Replies:
    11
    Views:
    1,124
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,145
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page