This question was asked to me..plz see

#include<stdio.h>

void change()

{

/*Write something in this function so that the output
of printf in main function should give 5 . Do not
change the main function */ }



void main()

{

int i=5;

change();

i=10;

printf("%d",i);

}

and i answered it like this....

This question is wrong(not possible).....
because even if somehow I change the value of i in change() function....locally in main() you r always giving i a value of 10 after change()...so as far as i feel it can't be done

Am i right

>Am i right
Barring sneaky tricks, you're right. But anything is possible if you're desparate enough to come up with a workaround that might work. For example:

#include <stdio.h>

void change()
{
#define printf(fmt,x) printf ( "%d\n", 5 )
}

int main()
{
  int i=5;

  change();
  i=10;
  printf("%d\n",i);

  return 0;
}

WOW...that's amazing

but what is fmt in # define

void change() {
  __asm {
  add word ptr[ebp+4],7
  }
}

:)

Pretty compiler-tied (VC++), but it's another answer...

commented: Thanks +1

>but what is fmt in # define
fmt and x replace "%d\n" and i in the printf call from main. Because they're not terribly important, the macro ignores them and uses its own complete call to printf. Since you know that the format string is what you want, you could also have done this:

#define printf(fmt,x) printf ( fmt, 5 )

Or even this if x is sure to be 10:

#define printf(fmt,x) printf ( fmt, x - 5 )

But it's generally a bad idea to redefine standard names, which is why this (and any other solution I can think of right now) is a sneaky hack.

void change() {
  __asm {
  add word ptr[ebp+4],7
  }
}

:)

Can you please explain/comment your code little....never done assembly in c++...but it seems very interesting

Can you please explain/comment your code little....never done assembly in c++...but it seems very interesting

Hard to short explain that. But I'll try to do in a single phrase:

Just before to call a subroutine, the address of the instruction following the call is saved, the way the CPU knows where to continue executing once the subroutine ends. By adding seven bytes to this saved value, we can force to skip that following instruction and go right to the next one. Please notice the number of bytes to skip depends on the way the main() function is coded.

That's the best I can say with no use of ASM jargon.

I understood your logic...but how did you calculate no of bytes.....and one more thing....please give me some good refrence of a link for studying assembly in C/C++...because i don't know the meaning of all the keywords used in your code

but how did you calculate no of bytes

Just looking at the at the dissasembly listing of the program. It's not a big deal.

please give me some good refrence of a link for studying assembly in C/C++...because i don't know the meaning of all the keywords used in your code

Hmmm...sincerely, I don't have that good reference. Never studied the subject that way. I just know ASM and also know that one can embed ASM code in most decent C/C++ compilers. The rest is pure syntax.

Just looking at the at the dissasembly listing of the program. It's not a big deal.

Ok..Ok... i got it...thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.