Same pointer parameter const and non-const

Discussion in 'C Programming' started by Old Wolf, Nov 3, 2009.

  1. Old Wolf

    Old Wolf Guest

    Is this program guaranteed to print 10, or can the compiler
    get 'confused' by the const and assume the variable
    still has its initial value?

    #include <stdio.h>

    int func(int *A, int const *B)
    {
    *A = 10;
    return *B;
    }

    int main()
    {
    int X = 5;
    printf("%d\n", func(&X, &X));
    }
     
    Old Wolf, Nov 3, 2009
    #1
    1. Advertising

  2. Old Wolf

    Seebs Guest

    On 2009-11-03, Old Wolf <> wrote:
    > Is this program guaranteed to print 10, or can the compiler
    > get 'confused' by the const and assume the variable
    > still has its initial value?


    I would say it has to print 10. There's no restrict anywhere in sight,
    there's questionable aliasing, so I think the compiler's obliged to
    assume that modifications of other objects with compatible types could
    potentially modify the thing pointed to by B.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 3, 2009
    #2
    1. Advertising

  3. Old Wolf

    DanDanDan Guest

    "Old Wolf" <> wrote in message
    news:...
    > Is this program guaranteed to print 10, or can the compiler
    > get 'confused' by the const and assume the variable
    > still has its initial value?
    >
    > #include <stdio.h>
    >
    > int func(int *A, int const *B)
    > {
    > *A = 10;
    > return *B;
    > }
    >
    > int main()
    > {
    > int X = 5;
    > printf("%d\n", func(&X, &X));
    > }


    The const will only mean that func won't set B, it won't assume anything
    else. The compiler won't get confused either, it will optimise away that
    shit completely.
     
    DanDanDan, Nov 4, 2009
    #3
  4. "DanDanDan" <> writes:

    > "Old Wolf" <> wrote in message
    > news:...
    >> Is this program guaranteed to print 10, or can the compiler
    >> get 'confused' by the const and assume the variable
    >> still has its initial value?
    >>
    >> #include <stdio.h>
    >>
    >> int func(int *A, int const *B)
    >> {
    >> *A = 10;
    >> return *B;
    >> }
    >>
    >> int main()
    >> {
    >> int X = 5;
    >> printf("%d\n", func(&X, &X));
    >> }

    >
    > The const will only mean that func won't set B, it won't assume anything
    > else.


    This wording is a little confusing. B is not const so the function is
    permitted to set B (it doesn't, but it might). In fact, there isn't a
    single const object anywhere in the program. The const is simply a
    promise that func won't use B to change the int that B points to.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Nov 4, 2009
    #4
    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. Dave
    Replies:
    4
    Views:
    400
    Old Wolf
    Apr 22, 2004
  2. Mark P
    Replies:
    6
    Views:
    360
    Victor Bazarov
    Aug 22, 2006
  3. Replies:
    4
    Views:
    284
    Mark P
    Nov 7, 2006
  4. fungus
    Replies:
    13
    Views:
    950
    fungus
    Oct 31, 2008
  5. asit
    Replies:
    3
    Views:
    283
    Juha Nieminen
    Nov 17, 2011
Loading...

Share This Page