I just happened to look at the prototype of the printf
(and other fprintf
class of functions) -
int printf(const char * restrict format, ...);
The keyword restrict
if I understand correctly disallows access to the same object through two pointers if one of them is marked restrict
.
An example that cites the same from the C standard is here.
One benefit of marking the format as restrict
I think is saving the function from the chance that the format string might get modified during the execution (say because of the %n
format specifier).
But does this impose a bigger constraint? Does this make the following function call invalid?
char format[] = "%s";
printf(format, format);
Because there is clearly an aliasing here. Why was the restrict
keyword added to the format
argument of printf
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…