#include <cstdio> char *names = "Windows\0System\0Config\0"; int main() { int l, r; for (char *name=names; *name; name+=r-l+1) if (printf("Folder: %n%s%n\n", &l, name, &(r=0)), !r) break; // Произошла ошибка, вероятно, стоит что-то сделать return 0; } It is clear that when feeding printf on user strings as a format, %n can do something wrong. But is there any harm from it in such a code in comparison with the variant in which it is not used?
#include <cstdio> #include <cstring> char *names = "Windows\0System\0Config\0"; int main() { for (char *name=names; *name; name+=strlen(name)+1) printf("Folder: %s\n", name); return 0; } PS: Based on the discussion in another answer .
printf(s), meaningprintf("%s", s). But such a code crashes the program without%n, with only one%s, and is a subscription for brainwashing from the authorities. - VladDifwithout checkingr, like soif ((rc = printf(...)) < 0) break;because, as they say in man -If an output error is encountered, a negative value is returned.. / I do not see any holes here. / And the difference between 1) and 2) is the first more efficiently, the work ofstrlen(almost for nothing?) Is done inprintf, but more difficult to understand (however, I personally am 1)). - avp