string is const char [] ?

jeroen dobbelaere xe44 7682 dobbelaj@access.bel.alcatel.be
Wed Jun 24 07:13:00 GMT 1998


Alexandre Oliva writes:
 > Kamil Iskra <kamil@dwd.interkom.pl> writes:
 > 
 > > On 23 Jun 1998, Alexandre Oliva wrote:
 > >> >   char a1[] = "test not const";
 > 
 > >> This line should cause egcs to flag an error.
 > 
 > > Excuse me?! I missed the original mail, but do you really suggest that all
 > > the programmers should suddenly switch to:
 > 
 > > char a1[]={'t', 'e', 's', 't', ' ', 'n', 'o', 't', ' ', 'c', 'o', 'n',
 > > 's', 't', '\0'};
 > 
 > How about:
 > 
 > const char a1[] = "test not const";


What if you want to initialise a changeable char array with a default string ?


// stupid example
void f()
{
  char counter[]="999";  // should work (normal initialization, size =4) 

  // char *counter="000";   // should at least give a warning, and
                         //should not work with this example

  for(;;)
  {
    if(counter[2]-- == '0')
    {
      counter[2]='9';
      if(counter[1]-- == '0')
      {
        counter[1]='9;
        if(counter[0]-- == '0')
        {
          counter[0]='9';

          break; // exit the for(;;)
	}
       }
     }

    // do something with the current  counter
    cout << counter << endl;
   }
}




More information about the Gcc-bugs mailing list