有可能解决这个问题吗?
最近我遇到了这个难题:
int main(){
int arr[7];
int b,c,d,a;
a=4;
printf("%d",arr[?]);
return 0;
}
问题是替换“?”使用整数,以便输出为 4。 我不确定,但我认为这不能以标准方式解决?! (不调用未定义的行为或取决于实现)如果否,那么我很想知道如何?
编辑:此任务取自此处,我试图解决10,但遗憾的是这不是问题提出者想要的答案。但是,我解决了它使用一些预先测试的依赖实现的mumbo-jumbo,但我真的没有解释它是如何工作的!
答案如下:剧透,欢迎您解释
Recently I encountered this puzzle :
int main(){
int arr[7];
int b,c,d,a;
a=4;
printf("%d",arr[?]);
return 0;
}
The question is to Replace "?" with a integer so that output is 4. I am not sure but I don't think this is solvable in a standard way ?! (Not invoking Undefined Behavior or depending on implementation) If NO, then I am very interested in knowing how ?
Edit:This task is taken from here, I tried to solve with 10, but sadly it's not the answer the problem setter wants.However, I solved it using some pretested implementation dependent mumbo-jumbo,but I really have no explanation for how it really works!
Here is the answer : SPOILER,You are welcome to explain it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在大多数实现中,arr[10](或 7)将为 4,因为局部变量将按顺序布置。
然而,这既没有定义也没有标准,并且不能依赖。
In most implementations,
arr[10]
(or 7) will be 4, since the locals will be laid out sequentially.However, this is neither defined nor standard, and must not be relied on.
假设没有符合标准的答案,你能用像arr[&a-arr]这样的操作(显然不是整数)吗?
编辑:感谢 Ben 和其他评论者的帮助,变得更加清晰。
Assuming there is no answer that conforms to the standard, could you use an operation (obviously not an integer) like arr[&a-arr]?
Edit: Made cleaner thanks to Ben and others in the comments.
我怀疑在某些系统上 10 可以做到这一点(取决于对齐和填充,以及 int 的大小),但这是未定义的行为。我看不到任何标准方法来完成所要求的操作。
I suspect on some systems 10 would do the trick (depending on alignment and padding, and the size of
int
), but that IS undefined behavior. I can't see any standard way to do what's asked.在 http://ideone.com 上:
优化器删除 b、c、d,因此 a 位于 7 处
on http://ideone.com:
optimizer removes b,c,d hence a right at 7
也许这是一个棘手的问题,也许有一个 4 但它不存在于数组中(所以也许一些奇怪的索引会给你一个 4 出于一个有趣的原因)。
Maybe it's a trick question, maybe there's a 4 but it doesn't exist in the array (so maybe some strange index will give you a 4 for an interesting reason).
我认为如果
?
需要替换为整数,则不可能有可移植的答案;但如果允许表达式,则可能存在可移植的解决方案;使用a [i] == i [a]
的事实。你需要大量的内存才能工作;出于同样的原因,我无法测试正确性。
I don't think a portable answer is possible if
?
needs to be replaced by an integer; but a portable solution may be possible if an expression is allowed; using the fact thata [i] == i [a]
.You'd need a huge amount of RAM to work; and I can't test the correctness for the same reason.
命令行说明:-)
cat 4130250.c
:输出“程序”的内容echo --------
:输出分隔符sed ...
:替换“?”使用“arr[0] = 4, 0”并写入标准输出<代码>| gcc -xc -:从标准输入(之前sed的输出)编译C
./a.out
:运行生成的二进制文件Explanation of the command line :-)
cat 4130250.c
: output the contents of the "program"echo --------
: output a separatorsed ...
: replace the "?" with "arr[0] = 4, 0" and write to standard output| gcc -xc -
: compile C from standard input (the output of the previous sed)./a.out
: run the produced binary