Inverted arguments in scanf()

前端 未结 3 1853
既然无缘
既然无缘 2021-02-19 14:28

I was (quickly) writing some code and accidently inverted the arguments in scanf():

char i[] = \"ABC1\\t\";
scanf(i, \"%s\");

Comp

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 14:45

    The manual entry for scanf (man scanf) gives the prototype:

    int scanf(const char *format, ...);
    

    A char[] is just a special type of char *, so the first argument is satisfied. Secondary arguments are evaluated at runtime (if I recall), so they aren't even considered by the compiler here. From the compiler's prospective, this is a fine call to the function given its prototype.

    Also, the compiler never checks whether you are trying to write to invalid locations. The great (or terrible) thing about C is that it will let you do more or less what you want, even if what you want is a bad idea.

提交回复
热议问题