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!
|