----- Original Message -----
From: "shashi kiran" <kiran_vee@yahoo.com>
To: "Vijay Kumar R Zanvar" <vijoeyz@hotpop.com>
Sent: Monday, February 09, 2004 8:40 PM
Subject: doubt

> what is diff between declaring
> const char *p and char const * p,
> is it allowed in C?

Yes.

> pls clear my doubt with some examples
>

Sure.  Read the following explaination -

              char   c;      /* c is a chararcter                             */
              char  *pc;     /* pc is a pointer to a char                     */
        const char  *pcc;    /* pcc is pointer to a const char                */
        char const  *pcc2;   /* pcc2, too, is a pointer to a const char       */
      char * const   cpc;    /* cpc is a const pointer to (non-const) char    */
const char * const   cpcc;   /* cpcc is a constant pointer to a constant char */


    The first two declarations are quite simple, and need no explaination.
To describe the rest of declarations, let us first consider the general form
of declaration:

    [qualifier] [storage-class] type [*[*]..] [qualifier] ident ;

                                or

    [storage-class] [qualifier] type [*[*]..] [qualifier] ident ;

where,

    qualifier: one of

            volatile
            const
            restrict (C99)

    storage-class: one of

            auto            extern
            static          register

    type:
            void            char            short
            int             long            float
            double          signed          unsigned
            _Bool           _Complex        _Imaginary      (C99)
            enum-specifier
            typedef-name
            struct-or-union-specifier

    Both the forms are equivalent.  Keyword in the brackets are optional.
By comparing the third and fourth declarations with the general form above,
we can say that they are equivalent.

    How to interpret then?  The simplest tip here is to notice the relative
position of the `const' keyword with respect to the asterisk (*).  Note the
following points:

    +   If the `const' keyword is to the left of the asterisk, and is the only
        such keyword in the declaration, then object pointed by the pointer
        is constant, however, the pointer itself is variable.  For example:

            const char * pcc;
            char const * pcc;

    +   If the `const' keyword is to the right of the asterisk, and is the only
        such keyword in the declaration, then the object pointed by the pointer
        is variable, but the pointer is constant; i.e., the pointer, once
        initialized, will always point to the same object through out it's
        scope.  For example:

            char * const cpc;

    +   If the `const' keyword is on both sides of the asterisk, the both
        the pointer and the pointed object are constant.  For example:

            const char * const cpcc;
            char const * const cpcc2;


   You can also follow the "nearness" principle; i.e.,

    +   If the `const' keyword is nearest to the `type', then the object
        is constant.  For example:

            char const * pcc;

    +   If the `const' keyword is nearest to the identifier, then the
        pointer is constant.  For example:

            char * const cpc;

    +   If the `const' keyword is nearest, both to the identifier and the
        type, then both the pointer and the object are constant.  For
        example:

            const char * const cpcc;
            char const * const cpcc2;

    However, the above method is more reliable.

--
Vijay Kumar R Zanvar