我遇到了这段代码:
char str[600];
scanf("%s", &str);
Run Code Online (Sandbox Code Playgroud)
当然,这会发出此警告:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
Run Code Online (Sandbox Code Playgroud)
我知道正确的方法是删除&
和键入scanf("%s", str)
.但它确实有效,所以我的问题是这是否会导致任何问题.是UB吗?当我切换str
到指针而不是数组时(显然)不起作用.但是这会在使用数组时引起任何问题吗?
(我在这里讨论了一个答案,答案者不想改变他对正确方法的回答)