背景
我被一位朋友问过以下谜题:
void fn(void)
{
/* write something after this comment so that the program output is 10 */
/* write something before this comment */
}
int main()
{
int i = 5;
fn();
printf("%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道可以有多个解决方案,一些涉及宏,一些假设有关实现和违反C.
我感兴趣的一个特定解决方案是对堆栈做出某些假设并编写以下代码:(我知道它是未定义的行为,但可能在许多实现中按预期工作)
void fn(void)
{
/* write something after this comment so that the program output is 10 */
int a[1] = {0};
int j = 0;
while(a[j] != 5) ++j; /* Search stack until you find 5 */
a[j] …Run Code Online (Sandbox Code Playgroud) 我们可以访问第m 个数组元素,如果它有m个元素吗?我的意思是,如果数组有7个元素,是否有可能在数组[7]中存储任何值?但是当长度为7时,数组索引从0开始并以6结束.