有可能解决这个问题吗?

发布于 2024-09-30 22:10:07 字数 566 浏览 5 评论 0原文

最近我遇到了这个难题:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

自找没趣 2024-10-07 22:10:07

在大多数实现中,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.

橘虞初梦 2024-10-07 22:10:07

假设没有符合标准的答案,你能用像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.

最后的乘客 2024-10-07 22:10:07

我怀疑在某些系统上 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.

平生欢 2024-10-07 22:10:07

http://ideone.com 上:

#include "stdio.h" 
int main(){
     int arr[7];
     int b,c,d,a;
     a=4;
     printf("%p %p %d %d",arr, &a, arr - &a, arr[7]);
   return 0;
}

0xbfa95918 0xbfa95934 -7 4

优化器删除 b、c、d,因此 a 位于 7 处

on http://ideone.com:

#include "stdio.h" 
int main(){
     int arr[7];
     int b,c,d,a;
     a=4;
     printf("%p %p %d %d",arr, &a, arr - &a, arr[7]);
   return 0;
}

0xbfa95918 0xbfa95934 -7 4

optimizer removes b,c,d hence a right at 7

他是夢罘是命 2024-10-07 22:10:07

也许这是一个棘手的问题,也许有一个 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).

影子的影子 2024-10-07 22:10:07

我认为如果 ? 需要替换为整数,则不可能有可移植的答案;但如果允许表达式,则可能存在可移植的解决方案;使用 a [i] == i [a] 的事实。

 printf("%d",arr[(unsigned long) ((a & (1 << ((sizeof (int) - 1) * CHAR_BIT))) ?
                                 /* Big Endian */
                                 memset (calloc ((unsigned long) arr + sizeof (int), 1),
                                         4, (unsigned long) arr + 1)
                                 :
                                 /* Small Endian*/
                                 memset (memset (malloc ((unsigned long) arr + sizeof (int)),
                                                 4, (unsigned long) arr + sizeof (int)),
                                         0,
                                         (unsigned long) arr + sizeof (int) - 1)
                                 )]);

你需要大量的内存才能工作;出于同样的原因,我无法测试正确性。

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 that a [i] == i [a].

 printf("%d",arr[(unsigned long) ((a & (1 << ((sizeof (int) - 1) * CHAR_BIT))) ?
                                 /* Big Endian */
                                 memset (calloc ((unsigned long) arr + sizeof (int), 1),
                                         4, (unsigned long) arr + 1)
                                 :
                                 /* Small Endian*/
                                 memset (memset (malloc ((unsigned long) arr + sizeof (int)),
                                                 4, (unsigned long) arr + sizeof (int)),
                                         0,
                                         (unsigned long) arr + sizeof (int) - 1)
                                 )]);

You'd need a huge amount of RAM to work; and I can't test the correctness for the same reason.

⊕婉儿 2024-10-07 22:10:07
$ cat 4130250.c \
> && echo -------- \
> && sed -e 's/\?/arr[0] = 4, 0/' 4130250.c | gcc -xc - \
> && ./a.out
int main(){
int arr[7],b,c,d,a;
a=4;
printf("%d\n", arr[?]);
return 0;
}
--------
<stdin>: In function `main`:
<stdin>:4: warning: incompatible implicit declaration of built-in function `printf`
4

命令行说明:-)

cat 4130250.c:输出“程序”的内容
echo --------:输出分隔符
sed ...:替换“?”使用“arr[0] = 4, 0”并写入标准输出
<代码>| gcc -xc -:从标准输入(之前sed的输出)编译C
./a.out:运行生成的二进制文件

$ cat 4130250.c \
> && echo -------- \
> && sed -e 's/\?/arr[0] = 4, 0/' 4130250.c | gcc -xc - \
> && ./a.out
int main(){
int arr[7],b,c,d,a;
a=4;
printf("%d\n", arr[?]);
return 0;
}
--------
<stdin>: In function `main`:
<stdin>:4: warning: incompatible implicit declaration of built-in function `printf`
4

Explanation of the command line :-)

cat 4130250.c: output the contents of the "program"
echo --------: output a separator
sed ...: 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文