Jump to content
xisto Community
cse-icons

C Code, Can U Solve This? Another interesting problem

Recommended Posts

How do I manipulate and change the value of i?

Technically you aren't changing the value of i -- you're defining a new i (it's at a new memory address). To answer your puzzle, you can change the value of i using an ASM injection attack using a noop sled to compensate for the variance in memory allocation addresses.

Share this post


Link to post
Share on other sites

void fun(void){  int * ptr =&i;  *ptr=50;}
Beat that.
This code wont work as the variable i is undeclared for fun()....it would have worked if "i" had been a global variable

There is another way if you wish to use pointers...


void fun(int *ptr){  *ptr=50;}int main(){int i = 20;fun(&i);printf("i = %d\n", i);return 0;}


the output will be 50
Edited by klusk (see edit history)

Share this post


Link to post
Share on other sites

#include <stdio.h>void fun(void){#define fun() i=10}int main(){int i = 20;fun();printf("i = %d\n", i);return 0;}
I think that this won't work :
(i guess) we can't put preprocessor directives on a function
If we can , it won't work also , because the compiler or the processor had completed the line :
fun();
so he won't replace it .

if the preprocessor works in functions i guess this will be the answer
void fun(void){#define printf("i = %d\n", i) i=0}int main(){int i = 20;fun();printf("i = %d\n", i);return 0;}

What do you think ???

Share this post


Link to post
Share on other sites

code for this program

C Code, Can U Solve This?

 

Easy code in 'C' program to prove pallidrome rule

 

Thanking you,

Vikram

 

 

 

 

 

 

 

 

 

-question by vikram

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.