tags:

views:

331

answers:

6

Now this is a silly puzzle I got from some exam paper,sadly I am unable to figure it out from last 15 minutes.

#include <stdio.h>

int main(void){

    /* <something> */    

      putchar(*(wer[1]+1));
   return 0;
 }

What should we replace in place of something in order to get the output e.Now we know putchar takes a int as argument but this code assumes to give a pointer.Does this question is even valid ?

+3  A: 
char * wer[] = { "foobar", "he"};
Kyle Butt
You can't initialize a pointer like this.
Alok
corrected. see above.
Kyle Butt
okay now... +1 coz u were the first to answer :P
Neeraj
Surely the canonical answer should be:char * wer[] = { "world !", "hello" };;-)
Paul R
+12  A: 
const char *wer[2] = { "failed", "test" };
Alok
+1 for failed test, and a good solution :-)
JonH
+7  A: 

Since a[i] is the same as *(a + i) by definition, you can transform the putchar() argument into wer[1][1]. So, something like char *wer[2] would be a satisfactory definition, and any values such that wer[1][1] == 'e' will work.

David Thornley
I upped this because you actually explained what should happen.
Liz Albin
+1,Nicely explained :).my version of solution :`char wer[][7] = {"dummy","he"};`
nthrgeek
A: 

First, in the code you have mentioned the argument to putchar() is

   *(wer[1]+1)

which is not a pointer. It seems that wer[1] is some pointer and that address pointed by wer[1] + 1 is dereferenced. So if wer is an array of pointers to int, then putchar argument should be int which is fine.

Now the code in place of something can be You have not mentioned clearly what does e mean, is e a char or e is 2.71... (Natural logarithm base) In either case it should be easy to get that output with this code.

-AD

goldenmean
A: 

From the very pedantic point of view, there's no correct answer to the question. The question is invalid. C language itself makes no guarantees about the output of a program if the output does not end in a newline character. See 7.19.2/2

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined.

This program output to the standard output, which is a text stream. The output of this program is implementation-dependent, regardless of what you put in place of /* <something> */, meaning that the question might make sense for some specific platform, but it makes no sense as an abstract C language question.

I highly doubt though that your examiners are expecting this kind of pedantry from you :)))

AndreyT
Why doesn't C make guarantees if the output does not end with a newline? Can you please refer to the Standard's section?
Johannes Schaub - litb
@Johannes Schaub: I was referring to the 7.19.2/2 which states that text stream might *require* a terminating new-line character at the end of the last line. Whether it is indeed required is implementation-defined.
AndreyT
A: 

An easy answer is:

   char **wer;
   putchar('e');
   return 0;

For example, the complete code would like like:

#include <stdio.h>

int main(int argc, char **argv)
{
    /* Something starts here */
    char **wer;
    putchar('e');
    return 0;
    /* Something ends here */

    putchar(*(wer[1] + 1));
    return 0;
}

The output is:

susam@swift:~$ gcc really-silly-puzzle.c && ./a.out && echo
e
susam@swift:~$

A more interesting question would have been: What is the shortest code that can replace /* */ to get the output 'e'?

Susam Pal