-
printf advanced!!
ok, as far as i know this is really advanced and i shouldnt be treading this path. but the curiosity gets the better of me and i am asking it anyway.
every function has an address, right? so now for basics let us consider the printf. now i want to know how to manipulate the values at this address so that:
each time somebody calls printf the same thing should be printed.
think this has got something to do with compiler design.
it would be nice if some real "punter" explained this to all us "pretty much" newbies. would be nicer if u could give the code snippet too.
thanx.
-
Can you be more specific?
-
ok here is what i wanted to achieve.
can u make printf to always print "hello world " no matter what the arguments u give to printf.
say suppose i call printf in any of these ways (included but not the only ones):
printf("this is a real world");
printf("%d",val);
printf(buf);
anyway u call printf anytime it should always print "hello world".
-
Code:
#include <stdio.h>
#define printf printf("Hello, world\n");
int
main(void)
{
int i = 0;
char *buf = "test";
printf("this is a real world");
printf("%d", i);
printf(buf);
return 0;
}
;)
-
do you need to #undef printf before doing that?
-
ok prelude ,
i din know that, thanx.
but actually prelude i wanted u to do that by changing things in the address of printf, man.
dont all functions have addresses like variables do (but unfortunately i dont know how to access them). i wanted u to make manipulations in those parts so that u get this effect.
ok becoz of ur previous reply one more question pops up. now what u r doing there is: each time the preprocessor sees printf it replaces ur printf with this one. ok now my question is what happens to the other part i.e the one in the brackets. is it ignored just like that? is everything that is inside two brackets and that is followed by a ; just ignored. if it is why dont we use them for commenting. thanx.
-
another addition:
you call printf from any program with any arguments it should print "Hello world". is that possible.
-
>but actually prelude i wanted u to do that by changing things in the address of printf, man.
I know what you wanted.
>dont all functions have addresses like variables do
Yes. You can have pointers to those addresses too.
>i wanted u to make manipulations in those parts so that u get this effect.
Let's look at this another way. Say you have an integer i with the value 0 and and integer j with the value 1. Using only the address of i and without changing the contents of that address, have any use of i actually use j. I suppose it's possible if you somehow switch the relative addresses of i and j with their symbols in the executable, but that's really beyond my present capability.
>ok now my question is what happens to the other part i.e the one in the brackets.
It's evaluated for a value and the value is discarded. Take printf("%d", i); for example. After preprocessing, it would be:
Code:
printf("Hello, world\n");("%d", i);
The ("%d", i); part is evaluated, returns i because of the comma operator, and the value is discarded because it isn't assigned anywhere. The statement is basically a no-op.
-
You can pass a function name acting as a pointer to that function.
For example:
Suppose you have two functions--printme and feedme.
Code:
int feedme(void (*function)(char*)){
(*function)("hi");
return 0;
}
void printme(char* value){
printf(value);
}
feedme(printme);
someone double-check me on that
-
>someone double-check me on that
I don't see a problem. But the original question was that any call to printf, not a pointer to a function resembling printf, would print "Hello, world". He basically wanted to rebind the address of printf to his own function defined elsewhere. :)