pointer to constant

Discussion in 'C Programming' started by dis_is_eagle@yahoo.com, Sep 5, 2006.

  1. Guest

    Hi.I have a doubt about the following program.

    int main()
    {
    char str[]="hello";
    const char* str1=str;
    strcpy(str1,"world");
    return 0;
    }
    I thought as str1 is constant, so copying world to it will give an
    error.But my compiler only shows a warning.Thanks for any help.

    Regards,
    Eric
    , Sep 5, 2006
    #1
    1. Advertising

  2. said:

    > Hi.I have a doubt about the following program.
    >
    > int main()
    > {
    > char str[]="hello";
    > const char* str1=str;
    > strcpy(str1,"world");
    > return 0;
    > }
    > I thought as str1 is constant, so copying world to it will give an
    > error.


    Since there is no prototype in scope for strcpy, the compiler is under no
    obligation to "know" what kinds of parameters it expects. It is certainly
    under no obligation to "know" that strcpy writes through the pointer you
    give it as its first parameter.

    If you #include <string.h> so that strcpy's prototype is provided, the
    compiler would then be *required* to issue a diagnostic message.

    > But my compiler only shows a warning.


    The compiler issued a diagnostic message. Just because it says "warning"
    rather than "error", that doesn't mean the code is fine. On the contrary,
    it means the compiler is worried. Maybe it's not worried enough to stop the
    compilation, but it's still fairly concerned, and so should you be.

    Don't think of warnings as "only a warning". Think of them as "uh-oh, I
    probably messed up big time - I'd better check the code and see what I did
    wrong." Modern compilers tend to give very good diagnostic messages.

    Sometimes, they do issue what we might consider to be "spurious" messages,
    but that doesn't happen very often. Unless you are **absolutely sure** that
    you know **exactly** why a warning message is spurious, treat it as an
    error, and fix your code accordingly.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 5, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > Hi.I have a doubt about the following program.
    >
    > int main()
    > {
    > char str[]="hello";
    > const char* str1=str;
    > strcpy(str1,"world");
    > return 0;
    > }
    > I thought as str1 is constant, so copying world to it will give an
    > error.But my compiler only shows a warning.Thanks for any help.


    The parameter of strcpy corresponding to str1 is not qualified with
    const, so the language rules can not prevent it from being modified.
    And this is not the point in your code. The warning in your code has
    the same meaning in line 9 in the following code. Assigning a non-const
    pointer to a const qualified pointer does not mean modifying the later
    one. So the compiler gives out warnings on line 8 & 9 but gives an
    errer on line 10 in the following example code.

    #include <string.h>

    int main(void){
    char str[] = "hello";
    const char *str1 = str;
    char *str2;

    strcpy(str1, "world"); /*line 8*/
    str2 = str1; /*line 9*/
    /* *str1 = 'a'; *//*line 10*/

    return 0;
    }
    , Sep 5, 2006
    #3
  4. Guest

    wrote:
    > wrote:
    > > Hi.I have a doubt about the following program.
    > >
    > > int main()
    > > {
    > > char str[]="hello";
    > > const char* str1=str;
    > > strcpy(str1,"world");
    > > return 0;
    > > }
    > > I thought as str1 is constant, so copying world to it will give an
    > > error.But my compiler only shows a warning.Thanks for any help.

    >
    > The parameter of strcpy corresponding to str1 is not qualified with
    > const, so the language rules can not prevent it from being modified.
    > And this is not the point in your code. The warning in your code has
    > the same meaning in line 9 in the following code. Assigning a non-const
    > pointer to a const qualified pointer does not mean modifying the later
    > one. So the compiler gives out warnings on line 8 & 9 but gives an
    > errer on line 10 in the following example code.
    >
    > #include <string.h>
    >
    > int main(void){
    > char str[] = "hello";
    > const char *str1 = str;
    > char *str2;
    >
    > strcpy(str1, "world"); /*line 8*/


    The strcpy call involves undefined behavior of course.

    > str2 = str1; /*line 9*/
    > /* *str1 = 'a'; *//*line 10*/
    >
    > return 0;
    > }
    , Sep 5, 2006
    #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. Fraser Ross
    Replies:
    4
    Views:
    1,027
    Fraser Ross
    Aug 14, 2004
  2. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,074
  3. Martin Magnusson
    Replies:
    2
    Views:
    492
    John Harrison
    Oct 8, 2004
  4. Tor Erik Soenvisen
    Replies:
    14
    Views:
    546
    Tim Roberts
    Nov 23, 2006
  5. G G
    Replies:
    3
    Views:
    79
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page