Nitin said:
Hi All,
Consider the following code snippet:
------------------------------------------
#include <stdio.h>
void change()
{
/* write something here so that the printf in main prints the value of
'i' as 10 */
}
int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}
------------------------------------------
apart from the trivial pre-processor trick like this:
#define printf(a,i) printf(a,2*i)
is there any way to achieve it ?
Thanks.
Hi Nitin,
here's a standard conformal solution.
It uses the pre-processor, though.
Nevertheless it's very bad coding style.
Never do this in production-level coding environments!!!!
#include <stdio.h>
#define change() change(int * pi)
void change()
{
*pi +=5;
}
#undef change
#define change() change_(&i)
int main(void)
{
int i=5;
change();
printf("%d\n",i);
return 0;
}
Your compiler has an option, which makes the pre-process replacements
visible. If you use GNU, it's "gcc -E source.c"
This will result in code like this:
....
void change(int * pi)
{
*pi +=5;
}
int main(void)
{
int i=5;
change(&i);
printf("%d\n",i);
return 0;
}
So, the preprocessor does, what you should have coded in the first place.
This might be a valuable help, if you had to work with legal code where you
don't want to modify a line, or you simply may not.
And, it could be helpful under debugging circumstances(instrumentation).
Bernhard