Can this be solved?

I recently ran into this riddle:

int main(){ int arr[7]; int b,c,d,a; a=4; printf("%d",arr[?]); return 0; } 

The question is to replace "?" with an integer, so the output is 4. I'm not sure, but I don't think this is solvable in the standard way ?! (Do not call Undefined Behavior or depending on implementation) If not, then I am very interested to know how?

Change This problem is taken from here , I tried to solve with 10, but, unfortunately, this does not answer the question that asks the problem. However, I decided using some preliminary checks, depending on mumbo-jumbo, but I really have no explanation how this works!

Here is the answer: SPOILER , you can explain it

+6
c ++ c puzzle
source share
7 answers

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.

+14
source share

In most implementations, arr[10] (or 7) will be 4, since the locales will be laid out sequentially.

However, this is not defined either standardly or rely on anything.

+15
source share

I suspect that on some systems 10 will do the trick (depending on alignment and padding and int size), but this behavior is undefined. I do not see any standard way to do what was asked.

+3
source share

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 

the optimizer removes b, c, d, therefore, has the right to 7

+1
source share

Maybe this is a trick, maybe there are 4, but it does not exist in the array (so maybe some weird pointer will give you 4 for an interesting reason).

0
source share

I don’t think a portable answer is possible if ? need to be replaced with an integer; but a portable solution may be possible if 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 will need a huge amount of RAM; and I cannot verify the correctness for the same reason.

0
source share
  $ 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

Command line explanation :-)

cat 4130250.c : displays the contents of the "program"
echo -------- : print separator
sed ... : replace "?" with "arr [0] = 4, 0" and write to standard output
| gcc -xc - | gcc -xc - : compile C from standard input (output from previous sed)
./a.out : run the generated binary

0
source share

All Articles