Discussion: Interview question?
Afficher un message
Vieux 01/02/2008, 22h20   #4
Antoninus Twink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Interview question?

On 1 Feb 2008 at 20:21, Padmat wrote:
> void main()
> {
> int i = 20;
> void fn();
> fn();
>
> printf ("%d",i);
> }
>
> void fn()
> {
> // add ur code only here so that i in main should print OTHER THAN 20
> }
>
> Is there any Leagal solution for this?
> Thanks,


Here is a solution for Linux/gcc-i386. I had to make i volatile to stop
gcc optimizing it away.


#include <stdio.h>

#define OFFSET 8 /* may need to change this depending on your compiler */

main()
{
volatile int i = 20;
void fn();
fn();

return printf ("%d\n",i);
}

void fn()
{
volatile int b;
*(&b + OFFSET)=42;
}

Happy hacking!

  Réponse avec citation
 
Page generated in 0,05919 seconds with 9 queries