On 5/13/15, Martin Sebor <msebor@xxxxxxxxxx> wrote: >> At the end of the day, it all comes down to passing format string as >> variable - the code below also does not produce error: >> >> const char *str = "Last printf: %B\n"; >> printf(str, 2); >> >> Can someone explain why this happens? > > Because with only a few exceptions (e.g., -Wuninitialized, > -Wstrict-aliasing, or -Wstrict-overflow), warnings are > implemented in an early stage of the translation process > before the compiler has done the sort of program analysis > or optimization necessary to "see" variable values. Ok, I see. > It only considers the values of literals and constants, as in: > > const char str[] = "Last printf: %B\n"; > printf (str, 2); // warning > > It doesn't do any sort of indirection so this won't cause > a warning either: > > const char* const str = "Last printf: %B\n"; > printf (str, 2); // no warning > > Martin > > But why is indirection done when string is defined as an array and not when it is a pointer to constant string?