Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug1776.cpp

1    #include <stdio.h>
2    #include <string.h>
3    #define N 5
4    struct Entry { char *flower; int amount; } catalog[N] =
5        { "daisy", 2, "rose", 0, "caledonia", 6, "pansy", 5, "lilac", 3 };
6    int cat_len = N;
7    void remove( int i )
8        {
9        cat_len--;
10       for( ; i < cat_len; i++ )
11           {
12           catalog[i].amount = catalog[i+1].amount;
13           strcpy( catalog[i].flower, catalog[i+1].flower );
14           }
15       }
16   int main()
17       {
18       remove(1);      // remove the flower whose amount is 0
19       printf( "Number of entries for %s is %d\n",
20                    catalog[1].flower, catalog[1].amount );
21       return 0;
22       }

A flower shop has a data base of all its flowers. In deleting an entry, the program appears to have created a strange hybrid, at least on one compiler. What's going on?


bug1776.cpp lint Output

--- Module:   bug1776.cpp
             _
    { "daisy", 2, "rose", 0, "caledonia", 6, "pansy", 5, "lilac", 3 };
bug1776.cpp(5) : Info 1776: Converting a string literal to char * is not const
    safe (initialization)
bug1776.cpp(5) : Info 1776: Converting a string literal to char * is not const
    safe (initialization)
bug1776.cpp(5) : Info 1776: Converting a string literal to char * is not const
    safe (initialization)
bug1776.cpp(5) : Info 1776: Converting a string literal to char * is not const
    safe (initialization)
bug1776.cpp(5) : Info 1776: Converting a string literal to char * is not const
    safe (initialization)

Reference Manual Explanation


1776  Converting a string literal to char * is not const safe
      (Context)  -- A string literal, according to Standard C++
      is typed an array of const char.  This message is issued
      when such a literal is assigned to a non-const pointer.
      For example:


                char *p = "string";


      will trigger this message.  This pointer could then be
      used to modify the string literal and that could produce
      some very strange behavior.

      Such an assignment is legal but "deprecated" by the C++
      Standard.  The reason for not ruling it illegal is that
      numerous existing functions have their arguments typed as
      char * and this would break working code.

      Note that this message is only given for string literals.
      If an expression is typed as pointer to const char in some
      way other than via string literal, then an assignment of
      that pointer to a non-const pointer will receive a more
      severe warning.

If you have comments or questions about this bug, please post them to our   Discussion Forum

Previous Bug - Bug #514 - April 2005

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2006, Gimpel Software, All rights reserved.