C Puzzle - 1


For the following code:

int fun()
{
     /*Write Code Here*/
}

int main()
{
     int i=10;
    fun();
    printf("%d\n",  i);
    return 0;
}


Now we have to change the value of i from 10 to 20 without changing anything in main(), we have to write all the operations in the fun() function.

Logic is that local variables are stored in the stack and stack always grows in downwards.

int fun()
{
   int j;
   int *ptr=&j;
   while (*ptr != 10) ptr--;
   *ptr =20;
    return 0;
}

Comments

Popular posts from this blog

Connecting Serial ports of two Ubuntu VMWare Virtual Machines

Linux Commands for Hardware Info - lshw, lspci, lsdev, lscpu, lsblk, lsusb

bb.utils.contains yocto