const and compiler warning

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bacbacdinner@gmail.com

    const and compiler warning

    Can anyone explain to me why the code below generates a warning?
    /////////////////////////////////////////////////////////////
    typedef struct testStruct * TestStructRef;
    typedef TestStructRef * TestStructRefPt r;
    void func(const TestStructRefPt r data);

    /* code that uses above */
    const TestStructRef junk;
    func(&junk); /* compiler warning about different 'const' qualifier,
    here &junk is a pointer to a pointer */
    //////////////////////////////////////////////////////////////

    If I change it to use let say a 'char' instead of a struct I don't get
    a compiler warning.

    //////////////////////////////////////////////////////////////
    void func2(const char ** data);

    /* code that uses above */
    const char * junk;
    func2(&junk); /* no compiler warning, here &junk is a pointer to a
    pointer */
    ///////////////////////////////////////////////////////////////

    I'm seeing the warning with Visual Studio 2005. Sorry in advance if
    it's something obvious but I'm at a loss to explain it. Any help
    would be greatly appreciated.
  • viza

    #2
    Re: const and compiler warning

    Hi

    On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
    Can anyone explain to me why the code below generates a warning?
    /////////////////////////////////////////////////////////////
    typedef struct testStruct * TestStructRef;
    typedef TestStructRef * TestStructRefPt r;
    void func(const TestStructRefPt r data);
    >
    /* code that uses above */
    const TestStructRef junk;
    func(&junk); /* compiler warning about different 'const' qualifier,
    here &junk is a pointer to a pointer */
    //////////////////////////////////////////////////////////////
    >
    If I change it to use let say a 'char' instead of a struct I don't get
    a compiler warning.
    >
    //////////////////////////////////////////////////////////////
    void func2(const char ** data);
    >
    /* code that uses above */
    const char * junk;
    func2(&junk); /* no compiler warning, here &junk is a pointer to a
    pointer */
    ///////////////////////////////////////////////////////////////
    I suggest:

    1: don't use CaMelCaps. It makes everything
    ReallyReallyDif fiCultToRead.

    2: don't typedef pointer types. typedef your struct, and then use *,
    which is universally understood.

    If you do this you may see the mistake that you have made. Hint:
    here &junk is a pointer to a pointer */
    computer says no.

    Also,
    const TestStructRefPt r foo;
    means
    TestStructRef * const foo;

    which is almost certainly not what you mean. Again, you would not
    have made this mistake if you followed 2 above.

    HTH

    viza

    Comment

    • bacbacdinner@gmail.com

      #3
      Re: const and compiler warning

      On Jun 12, 4:32 am, viza <tom.v...@gmail .comwrote:
      Hi
      >
      On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
      >
      >
      >
      Can anyone explain to me why the code below generates a warning?
      /////////////////////////////////////////////////////////////
      typedef struct testStruct * TestStructRef;
      typedef TestStructRef * TestStructRefPt r;
      void func(const TestStructRefPt r data);
      >
      /* code that uses above */
      const TestStructRef junk;
      func(&junk); /* compiler warning about different 'const' qualifier,
      here &junk is a pointer to a pointer */
      //////////////////////////////////////////////////////////////
      >
      If I change it to use let say a 'char' instead of a struct I don't get
      a compiler warning.
      >
      //////////////////////////////////////////////////////////////
      void func2(const char ** data);
      >
      /* code that uses above */
      const char * junk;
      func2(&junk); /* no compiler warning, here &junk is a pointer to a
      pointer */
      ///////////////////////////////////////////////////////////////
      >
      I suggest:
      >
      1: don't use CaMelCaps. It makes everything
      ReallyReallyDif fiCultToRead.
      >
      2: don't typedef pointer types. typedef your struct, and then use *,
      which is universally understood.
      >
      If you do this you may see the mistake that you have made. Hint:
      >
      here &junk is a pointer to a pointer */
      >
      computer says no.
      >
      Also,
      const TestStructRefPt r foo;
      means
      TestStructRef * const foo;
      >
      which is almost certainly not what you mean. Again, you would not
      have made this mistake if you followed 2 above.
      >
      HTH
      >
      viza
      Thanks for the reply. I have no choice as far as using CamelCase
      since that's the standard where I work. What I'm trying to do is call
      a function that returns a pointer to a constant structure. I changed
      the example using the advice you gave to:

      struct testStruct;
      typedef struct testStruct TestStruct;

      void func(const TestStruct ** data);

      const TestStruct * pData = 0;
      func(&pData);

      Comment

      • Ian Collins

        #4
        Re: const and compiler warning

        bacbacdinner@gm ail.com wrote:
        On Jun 12, 4:32 am, viza <tom.v...@gmail .comwrote:
        //////
        >I suggest:
        >>
        >1: don't use CaMelCaps. It makes everything
        >ReallyReallyDi ffiCultToRead.
        >>
        >2: don't typedef pointer types. typedef your struct, and then use *,
        >which is universally understood.
        >>
        >If you do this you may see the mistake that you have made. Hint:
        >>
        >
        Thanks for the reply. I have no choice as far as using CamelCase
        since that's the standard where I work. What I'm trying to do is call
        a function that returns a pointer to a constant structure.
        Camel case is just a common choice of style.

        The use of typedefs for pointer types is just bad style!

        --
        Ian Collins.

        Comment

        • santosh

          #5
          Re: const and compiler warning

          Ian Collins wrote:

          [ ... ]
          The use of typedefs for pointer types is just bad style!
          One might make an exception to that when constructing opaque data types.
          This is a situation where you want to hide the pointer nature and
          present it as a handle.

          Comment

          • christian.bau

            #6
            Re: const and compiler warning

            On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
            Can anyone explain to me why the code below generates a warning?
            /////////////////////////////////////////////////////////////
            typedef struct testStruct *   TestStructRef;
            typedef TestStructRef *  TestStructRefPt r;
            void func(const TestStructRefPt r data);
            Function func has one parameter of type "TestStructRefP tr". The fact
            that you wrote "const" in front of it is completely pointless and
            doesn't mean anything, because qualifiers are ignored in a function
            declaration. If you used the same "const" in a function definition,
            then it would mean that the variable "data" is const and cannot be
            modified; for example, you wouldn't be allowed to write "data =
            NULL;".

            You seem to believe that "const TestStructRefPt r" is the same as
            "const TestStructRef *". It isn't. The former is a pointer to
            TestStructRef, and the pointer itself cannot be modified. The latter
            is a pointer to TestStructRef, and the TestStructRef pointed to cannot
            be modified.

            Comment

            • CBFalconer

              #7
              Re: const and compiler warning

              "christian. bau" wrote:
              bacbacdin...@gm ail.com wrote:
              >
              >Can anyone explain to me why the code below generates a warning?
              >/////////////////////////////////////////////////////////////
              >typedef struct testStruct * TestStructRef;
              >typedef TestStructRef * TestStructRefPt r;
              >void func(const TestStructRefPt r data);
              >
              Function func has one parameter of type "TestStructRefP tr". The
              fact that you wrote "const" in front of it is completely ...
              Besides, he is probably not using a C99 compiler, so that line of
              division symbols is an automatic syntax error. :-)

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.


              ** Posted from http://www.teranews.com **

              Comment

              • bacbacdinner@gmail.com

                #8
                Re: const and compiler warning

                On Jun 12, 12:49 pm, "christian. bau"
                <christian....@ cbau.wanadoo.co .ukwrote:
                On Jun 12, 3:00 am, bacbacdin...@gm ail.com wrote:
                >
                Can anyone explain to me why the code below generates a warning?
                /////////////////////////////////////////////////////////////
                typedef struct testStruct * TestStructRef;
                typedef TestStructRef * TestStructRefPt r;
                void func(const TestStructRefPt r data);
                >
                Function func has one parameter of type "TestStructRefP tr". The fact
                that you wrote "const" in front of it is completely pointless and
                doesn't mean anything, because qualifiers are ignored in a function
                declaration. If you used the same "const" in a function definition,
                then it would mean that the variable "data" is const and cannot be
                modified; for example, you wouldn't be allowed to write "data =
                NULL;".
                >
                You seem to believe that "const TestStructRefPt r" is the same as
                "const TestStructRef *". It isn't. The former is a pointer to
                TestStructRef, and the pointer itself cannot be modified. The latter
                is a pointer to TestStructRef, and the TestStructRef pointed to cannot
                be modified.
                The behavior I wanted was the latter. Thanks for the help, very much
                appreciated.

                Comment

                • bacbacdinner@gmail.com

                  #9
                  Re: const and compiler warning

                  On Jun 12, 12:23 pm, santosh <santosh....@gm ail.comwrote:
                  Ian Collins wrote:
                  >
                  [ ... ]
                  >
                  The use of typedefs for pointer types is just bad style!
                  >
                  One might make an exception to that when constructing opaque data types.
                  This is a situation where you want to hide the pointer nature and
                  present it as a handle.
                  The intent is to construct an opaque data type but it looks like using
                  a typedef to a pointer isn't the way to go.

                  Comment

                  • Ben Bacarisse

                    #10
                    Re: const and compiler warning

                    bacbacdinner@gm ail.com writes:
                    On Jun 12, 12:23 pm, santosh <santosh....@gm ail.comwrote:
                    >Ian Collins wrote:
                    >>
                    >[ ... ]
                    >>
                    The use of typedefs for pointer types is just bad style!
                    >>
                    >One might make an exception to that when constructing opaque data types.
                    >This is a situation where you want to hide the pointer nature and
                    >present it as a handle.
                    >
                    The intent is to construct an opaque data type but it looks like using
                    a typedef to a pointer isn't the way to go.
                    Then you may be giving up too early. If you need to point to a
                    constant and typedef the whole lot (because you do want it to be just
                    an opaque handle) you need to put the const into the typedef. You
                    can't add constness to the to pointed-to type once the typedef is
                    defined (as you discovered). Thus:

                    typedef const struct my_hidden_struc t *Handle;

                    Allows one to declare, for example,

                    void function(Handle h);

                    and the struct pointed to by h is const. Of course, since the struct
                    is opaque, your users can't get at the members pointed to by one of
                    these handles, so all you gain by making it const is to prevent
                    accidental copying like: '*h = *other_handle;' .

                    --
                    Ben.

                    Comment

                    Working...