954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

scope problem again

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

>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;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

WOW...that's amazing

but what is fmt in # define

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
void change() {
  __asm {
  add word ptr[ebp+4],7
  }
}


:)

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

Alvein
Junior Poster
104 posts since Jul 2005
Reputation Points: 12
Solved Threads: 4
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
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.

Alvein
Junior Poster
104 posts since Jul 2005
Reputation Points: 12
Solved Threads: 4
 

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
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.

Alvein
Junior Poster
104 posts since Jul 2005
Reputation Points: 12
Solved Threads: 4
 
Just looking at the at the dissasembly listing of the program. It's not a big deal.

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You