This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C: Proposal: Warning using char* a; if(a == "")


> I will help out in this case of leak of imagination:
> 
>  void gui_paned_menu_callback_function(Widget *w)
>  {
> 
> 	char *label;
>         label = gui_get_the_label(w);
> 	if (label == "BANG")
> 		printf("OK\n");
> 	else
> 		printf("BOOM\n");
>  }
> 
> 
> 
>  int main(int argc, char *argv[]) 
>  {
> 	Widget *item;
> 	item = create_menu_item("BANG");
>         add_callback(item, gui_paned_menu_callback_function);
>  }
> 
> Perfectly valid C and makes perfect sense on platforms which collate
> constant strings. 

And if some later programmer does not notice that you are doing this
and separates main and gui_paned_menu_callback_function into two
separate files, the program breaks and no one knows why.  For this
reason, good programmers do not use such tricks.  Rather, they replace
the occurrences of "BANG" with something like

static const char bang_text[] = "BANG";

and they now have a portable program that doesn't mysteriously break.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]